tags:

views:

138

answers:

4

Hi,

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?

+13  A: 

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

Vinko Vrsalovic
no. Should I be doing this?
daniel
Yes, you should.
Vinko Vrsalovic
@daniel - yes. It calls up the session from storage.
Aiden Bell
thanks guys. checks out.
daniel
yes. you should really read the section about sessions in the manual before starting to code.
Philippe Gerber
@Vinko Vrsalovic: Better make your anwer a statement rather thant a question.
Gumbo
@Gumbo: Ok... ...
Vinko Vrsalovic
+4  A: 

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.

cletus
+1  A: 

You must have session_start() on every page

Yoann Le Touche
+1  A: 

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

Neil Aitken