Here are the code of my login page where the login script checks for the authenticity of the user and then redirects to inbox page using header function.
<
The other important reason sessions can not work is playing with the session cookie settings, eg. setting session cookie lifetime to 0 or other low values because of simple mistake or by other developer for a reason.
session_set_cookie_params(0)
If you use a connection script, dont forget to use session_start(); at the connection too, had some trouble before noticing that issue.
Maybe it helps others, myself I had
session_regenerate_id(false);
I removed it and all ok!
after login was ok... ouch!
Just talked to the hosting service, it was an issue at their end. he said " your account session.save_path was not set as a result issue arise. I set it for you now."
And it works fine after that :)
- Make sure
session_start();is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening<?phptag before anything else. Also ensure there are no whitespaces/tabs before the opening<?phptag.- After the
headerredirect, end the current script usingexit();(Others have also suggestedsession_write_close();andsession_regenerate_id(true), you can try those as well, but I'd useexit();).- Make sure cookies are enabled in the browser you are using to test it on.
- Ensure
register_globalsis off, you can check this on thephp.inifile and also usingphpinfo(). Refer to this as to how to turn it off.- Make sure you didn't delete or empty the session.
- Make sure the key in your
$_SESSIONsuperglobal array is not overwritten anywhere.- Make sure you redirect to the same domain. So redirecting from a
www.yourdomain.comtoyourdomain.comdoesn't carry the session forward.- Make sure your file extension is
.php(it happens!).
PHP session lost after redirect
I had the same issue for a while and had a very hard time figuring it out. My problem was that I had the site working for a while with the sessions working right, and then all of the sudden everything broke.
Apparently, your session_save_path(), for me it was /var/lib/php5/, needs to have correct permissions (the user running php, eg www-data needs write access to the directory). I accidentally changed it, breaking sessions completely.
Run sudo chmod -R 700 /var/lib/php5/ and then sudo chown -R www-data /var/lib/php5/ so that the php user has access to the folder.