0

In my login php page, after I check if the user's info is saved in the database, I set a session:

$_SESSION['username'] = $user;
                if (isset($_SESSION['username'])) {
                    header( 'Location: index.php' );
                }

and put session_start(); on this page at the tippy top.

Then it redirects me to index.php, telling me that the session has been set. On this page, I put session_start(); at the top but in the login area, I type:

<?php if (!isset($_SESSION['username'])) { echo $_SESSION['username'];?><a href="/login.php"><li class="cat_0" id="login_btn_1">Login / SignUp</li></a>
        <?php }
            else {?>
                <span id="login_show"><a href="/account/user.php?u=<?php echo $_SESSION['username'];?>" id="username_btn"><?php echo $_SESSION['username']; ?><a href="/account/logout.php?logout=1" id="logout_btn">LOGOUT</a></a></span>
            <?php }?>

but every time, even if I reload, it shows the result for the !isset(), so that is telling em the session variable is not set. I check in my chrome cookies settings and it shows that PHPSESSID is set each time I test the Login. Can anyone explain why my session is not starting or what the problem is?

Bruce
  • 1,647
  • 4
  • 20
  • 22
TheGodProject
  • 147
  • 2
  • 12
  • are you on a load balanced server? if you are then read [this](http://stackoverflow.com/questions/994935/php-sessions-in-a-load-balancing-cluster-how) – DevDonkey Jul 29 '15 at 09:55

2 Answers2

0

This is probably due to a simple race condition between your script and your session handling.

$_SESSION is a superglobal which has a specific way of working.

You are actually trying to access a superglobal variable which still only exists in the buffer of the session handler. If you want to access the session variable, you need to write the buffered data to the session by calling session_write_close() first:

$_SESSION['username'] = $user;
session_write_close(); // remember, you can no longer write to the sessions any more
if (isset($_SESSION['username'])) {
    header( 'Location: index.php' );
    exit; // just for safety
}
Repox
  • 15,015
  • 8
  • 54
  • 79
0
On the login page
-----------------
session_start();
if( !isset( $_SESSION['usename'] ) && isset( $user ) ) $_SESSION['username'] = $user;
if( isset( $_SESSION['username'] ) ) header( 'Location: index.php' );

On the index page
-----------------
if ( isset( $_SESSION['username'] ) ) { 
    echo '<span id="login_show">
            <a href="/account/user.php?u='.$_SESSION['username'].'" id="username_btn">'.$_SESSION['username'].'</a>
            <a href="/account/logout.php?logout=1" id="logout_btn">LOGOUT</a>
        </span>';
} else {
    echo "
        <a href='/login.php'>
            <li class='cat_0' id='login_btn_1'>Login / SignUp</li>
        </a>";
}

There was an error in the html - there were two closing a tags together and quite often badly formed html can do all sorts of weird things to the display of the page.

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46