daniel
daniel

Reputation: 1222

$_SESSION variables not transferring from page to page

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?

Upvotes: 0

Views: 540

Answers (4)

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340171

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

Upvotes: 13

Neil Aitken
Neil Aitken

Reputation: 7854

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

Upvotes: 1

Yoann Le Touche
Yoann Le Touche

Reputation: 1300

You must have session_start() on every page

Upvotes: 1

cletus
cletus

Reputation: 625007

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.

Upvotes: 4

Related Questions