So I’m trying to get a simple session based login working, and my sessions aren’t being saved at all. I’ve tried many things, but nothing works! Please help!
Edit: added start_session, but it is still empty
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
…
$row = mysql_fetch_row($sql_result);
$_SESSION['id']= $row[0];
$_SESSION['uname'] = $row[1];
$_SESSION['pass'] = $row[2];
session_write_close(); <-- now removed, but the session is still empty
echo $row[1] . "Login accepted.<br />";
This is the code to create the session. all the rows are accounted for, and the code is being called, but the session still turns out empty.
if(empty($_SESSION['uname'])) echo 'empty session';
Also, to note that firebug says 0 objects stored in sessionstorage.
Remove
session_write_close()session_write_close()ends the session:Any subsequent calls to
$_SESSIONwill not return any data as the session has been written and then closed.Generally you would not call
session_write_close()until just before you perform aheader('Location: http://example.org')redirect. As this is not the case in your code you should remove it.Ensure you are calling
session_start()I am assuming that you are calling
session_start()at the top of all your scripts.Test your session
To test that your session is working then I suggest implementing the following test script:
This way you know that there is definitely something in your session array. In your sample code your are getting your data from a database, which might be returning empty values.
PHP session and HTML5 sessionstorage confusion
They are not one and the same thing. PHP’s sessions are stored on the server whereas the browser stores HTML5 sessionstorage locally in the browser. The HTML5 sessionstorage is a client side store of information that only lasts for that session. See the w3c specification for more information.
PHP’s
$_SESSIONdoes not get stored or affect data in the HTML5 sessionstorage.