Unable to load PHP session information

advertisements

I am trying to make a log-in system in PHP. Ive got all the database stuff working, but trying to save and load the session, so the user is logged in as they navigate my site is not working. I set the session info like so:

session_start();
...database code
if($count==1){
//log in info correct, save to session and redirect back.
$_SESSION['username'] =$username;
$_SESSION['loggedin'] = "true";

header("location:../index.php");
}

Then, back on the index page.

  <?php
  session_start();
  if(!isset($_SESSION['loggedin'])){
  echo "You must be logged in!";
  }else{
  echo "You are logged in";
  }
?>

This code all happens at the top of the page, before any html. Even after logging in, it says "You Must be Logged In" Any help would be great :) Thanks


I tried this, seems to work find. Just declare the variable before the if($count ... and change isset to empty.

        $_SESSION['loggedin'] = "";

        if($count == 1)
        {
            /*same thing */
        }

        if(!empty($_SESSION['loggedin']))
        {
            echo "You are logged in";
        }else
        {
            echo "You must be logged in!";
        }

Also imo, I think it will be better to put the header where the echo is.