简体   繁体   中英

session_start(): cannot start session when headers already exist

Here is my exact error: Warning: session_start(): Cannot start session when headers already sent in /storage/ssd3/130/18071130/public_html/header.php on line 2

I am try to make a login system that changes the header to logout / profile page when logged in.

  • this works on a locally hosted server perfectly, but when I upload it to a website I get the error. (specifically 000webhost)

here is my header.php code

<?php
session_start();
?>


<link href="reset.css" rel="stylesheet">
<link href="bootstrap.css" rel="stylesheet">
<link href="style.css" rel="stylesheet"> 
<link href="finalstyle.css" rel="stylesheet"> 
<nav class="signup-navbar">
<div class="navbar" style="font-size:200%; ; font-family:Lobster; word-spacing:10px;">
<a href="index.php" alt="Blogs logo"></a>
<ul>
    

        <?php
        if (isset($_SESSION["useruid"])) {
            echo '<button style="float:right; font-family: Lobster; margin-right:20px; margin-bottom: 10px;">';
            echo '<li style="display:inline; float:right;"><a href="profile.php"> Profile Page</a></li>';
            echo '</button>';
            echo    '<button style="float:right; font-family: Lobster; margin-right:20px; margin-bottom: 10px;">';
            echo    '<li style="display:inline; float:right;"><a href="includes/logout.inc.php"> Log Out </a></li>';
            echo    '</button>';
        }
        else {
            echo '<button style="float:right; font-family: Lobster; margin-right:20px; margin-bottom: 10px;">';
            echo '<li style="display:inline; float:right;"><a href="signup.php"> Sign Up </a></li>';
            echo '</button>';
            echo    '<button style="float:right; font-family: Lobster;margin-right:20px; margin-bottom: 10px;">';
            echo    '<li style="display:inline; float:right;"><a href="login.php"> Log In </a></li>';
            echo    '</button>';
        }
        ?>

</ul>
</div>
</nav>

Encode PHP files as UTF-8 without BOM. It seems like an error that occurred because the coding of the pages was wrong.

Also at the top of the page ob_start() ; I recommend you to use it.

and write the line that says session_start at the top. Make sure it's on line 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM