Session not working

Hi,
In my code, whenever I am trying to access a session variable created in a page(say page1) in another page(say page2), it is lost. In both the pages,I have explicitly started session though. Can someone tell me why so happening? Thanks in advance.

Post your code please

Page1

session_start();
 ob_start();
 $_SESSION['value1'] = 'testing';
 echo $_SESSION['value1'];

Page2

session_start();
 ob_start();
 //$_SESSION['value1'] = 'testing';
 echo $_SESSION['value1'];

Okay your session is working fine. You’re Buffering your output but not…outputing it. Flush the buffer (ob_end_flush()) and see if you get what you need.

Why are you ob_start’ing, anyway?

Lion,
Thanks for your reply. I have done what you said…but it doesn’t seems to work. Thanks again.

Does anyone has any idea about this? Thanks Again.

I suspect it’s most likely the output buffering messing it up. If there’s any way to avoid using it, don’t. It may mean you have to put a little more thought into your code’s logic, but IMHO it wil be worth the effort.

Anyway, I doubt if you’re using IE6, but if you are try:

<?php
session_start();
header("Cache-control: private"); //IE 6 Fix

Thanks Mittineague…I have tried yours but doesn’t seem to work.

achintya, in your phpinfo() what are the settings in the sessions section?

Thanks SpacePhoenix. Following are the settings:

session.auto_start	Off	Off
session.bug_compat_42	Off	Off
session.bug_compat_warn	On	On
session.cache_expire	180	180
session.cache_limiter	nocache	nocache
session.cookie_domain	no value	no value
session.cookie_httponly	Off	Off
session.cookie_lifetime	0	0
session.cookie_path	/	/
session.cookie_secure	Off	Off
session.entropy_file	no value	no value
session.entropy_length	0	0
session.gc_divisor	1000	1000
session.gc_maxlifetime	1440	1440
session.gc_probability	1	1
session.hash_bits_per_character	5	5
session.hash_function	0	0
session.name	PHPSESSID	PHPSESSID
session.referer_check	no value	no value
session.save_handler	files	files
session.save_path	/var/www/vhosts/mydomain.com/httpdocs/tmp/	/var/lib/php/session
session.serialize_handler	php	php
session.use_cookies	On	On
session.use_only_cookies	Off	Off
session.use_trans_sid	0	0

As far as the save_path is concerned, i have changed the first one using ini_set function. Thanks again.

Did you change the save_path before or after the problem with sessions started to occur?

I have changed the save_path after the issue started. Thanks again.

It seems to be correct…Create a new page. Put this code in it. Run it in your browser, and tell me what it says?


<?php
session_start();
echo session_id();
?>

It says something like this…
sjgf6cfrr8rqjl0oea3366qpd6

That looks like it could be a session hash. My phpinfo() has a few differences from yours
yours

...
session.bug_compat_42	Off	Off
...
session.gc_divisor	1000	1000
...
session.hash_bits_per_character	5	5
...
session.save_path	/var/www/vhosts/mydomain.com/httpdocs/tmp/	/var/lib/php/session
...

mine

...
session.bug_compat_42	On	On
...
session.gc_divisor	100	100
...
session.hash_bits_per_character	4	4
...
session.save_path	\\xampplite\	mp	\\xampplite\	mp
...

TBH I don’t know what the importance of the first 3 are off the top of my head, I’d need to look into them. But the save_path is significant because I have Windows OS that uses back slashes.

Again, why are you using output buffering? If it’s only to avoid “already sent” errors try rewriting your code.

Yeah, sounds like the session ID is being created fine…

You’re not doing something silly like destroying the session at the end of page 1, are you? I think we’re missing seeing something in your code because you have not posted most/all of it.

No not at all…I have posted all of it.

If you echo session_id() on page2, does it give you the same answer as page1?

No it displays a different value from first page…

So the problem is that the session isnt retaining.

  1. Page1 is on the same website as page2?
  2. Have you enabled cookies on your browser?