Tutorial :$_SESSION variables not transferring from page to page



Question:

If I write the following code:

session_start();  $_SESSION['user_id']='daniel';  

the variable stays fine as long as I'm on the page on which it was created, and the second I try to call $_SESSION['user_id'] from another page, I don't get a response. Can anyone tell me what mistake I'm making?


Solution:1

You should be using session_start() on every page you want to use sessions on.


Solution:2

As long as:

  • You are doing session_start() on the other page. Note: you don't make this call once. You do it on every page that wants to access the session information;
  • The other page can see your cookie from this site (ie sufficiently similar domain); and
  • The other page is running on the same server.

then it can see it. Construct a simple test case and verify this and then work out why what you're doing is different.


Solution:3

You must have session_start() on every page


Solution:4

Ensure that the PHPSESSID cookie is actually being set, and that no headers / content have been sent before you call session_start()


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »