How do I resolve the problem of losing a session after a redirect in PHP?
Recently, I encountered a very common problem of losing session after redirect. And after searching through this website I can still find no solution (although this came the closest).
Update
I have found the answer and I thought I'd post it here to help anyone experiencing the same problem.
Make sure
session_write_close
is not called betweensession_start()
and when you set your session.I ran into this issue on one particular page. I was setting $_SESSION values in other pages right before redirecting and everything was working fine. But this particular page was not working.
Finally I realized that in this particular page, I was destroying the session at the beginning of the page but never starting it again. So my destroy function changed from:
to:
And everything worked!
I've been struggling with this for days, checking/trying all the solutions, but my problem was I didn't call
session_start();
again after the redirect. I just assumed the session was 'still alive'.So don't forget that!
I had the same problem. I worked on it for several hours and it drove me crazy.
In my case the problem was a 404 called due to a missing favicon.ico in Chrome and Firefox only. The other navigators worked fine.
I fixed by giving group write permissions to the path where PHP store session files. You can find session path with session_save_path() function.
I also had the same issue with the redirect not working and tried all the solutions I could find, my header redirect was being used in a form.
I solved it by putting the header redirect in a different php page 'signin_action.php' and passing the variables parameters through I wanted in url parameters and then reassigning them in the 'signin_action.php' form.
signin.php
signin_action.php
It is not a beautiful work-around but it worked.