0

I have a website that contains a login and when the user is logged in I want it to display a message on the main page as well as carry the session on through.

I am receiving the error:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by..

and

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

When I am not logged in it displays the error, but as soon as I log in and return to the page it disappears, but doesn't display the "You are logged in!" like it should...

Here is my authentication code after it checks the database for a match:

if ($result["userName"] == $_POST[txtUsername] && $result["password"] ==$_POST[txtPassword])
    {
        ?>
        <script type="text/javascript">;
        alert("successfully logged in, <?php echo $result['userName'];?>!");
        window.location.href='http://manning3.jcu.edu.au/~jc192699/index.html';
        </script>
        <?php
        session_start();
        $_SESSION["access"] = 'granted';
    }

And my code in my index.php that seems to cause the error:

 <?php
    session_start();
    if ($_SESSION["access"] == 'granted'){
        ?>
        <p>You are logged in!</p>
        <?php
    }
    else{
    ?>
    <p>You are not logged in!</p>
    <?php
    }
    ?>

Any help would be GREATLY appreciated. Thank you!

DommyCastles
  • 435
  • 3
  • 8
  • 21
  • Somewhere in your code, a loose space (often at the end of a file) is causing output to trigger before your call to `session_start`. Check the myriad related questions in the right column for more places to look... – rjz May 11 '12 at 00:04
  • no matter what you do with sessions, the session_start() has to be the first thing you are doing. – thetrompf May 11 '12 at 00:06
  • Thank you for your help! The errors are now gone, but I still have a problem... After I log in and return to the index page, the system doesn't display "You are logged in!". Any clues? – DommyCastles May 11 '12 at 00:21

2 Answers2

2

session_start() must be invoked prior to any output being printed to the browser. Because you print a javascript script prior to starting the session, headers have already been set, so a session cannot be be started.

FThompson
  • 28,352
  • 13
  • 60
  • 93
  • Thank you for your help! The errors are now gone, but I still have a problem... After I log in and return to the index page, the system doesn't display "You are logged in!". Any clues? – DommyCastles May 11 '12 at 00:20
  • Cookies work great for login. Simply store a cookie containing whether or not the user is logged in, and set an expiration date on it. Then, for your logout, you would expire that cookie by setting its expiration date to a date in the past. Also, on a side note, you should press accept on the answer you accept when asking a question; doing so lets other users know how you solved the issue you encountered. – FThompson May 11 '12 at 00:24
  • Any ideas on how I can go about doing this? – DommyCastles May 11 '12 at 00:25
  • Yes; [here](http://stackoverflow.com/questions/10526967/how-to-unset-cookie-in-php)'s a question related to cookies that basically shows how to do this. – FThompson May 11 '12 at 00:45
1

It looks like you have a space before your <?php declaration on line 1. This will cause headers to be sent before session_start() and might be causing the error you're receiving.

Jon Friskics
  • 292
  • 2
  • 9