Link to home
Start Free TrialLog in
Avatar of Bernard Savonet
Bernard SavonetFlag for France

asked on

Problem with PHP SESSION variables changes

Short form: what might lead $_SESSION content to change between several PHP pages?

Background:
1 - In a php page, I add values to $_SESSION, then session_write_close() [note: values were successfully written in $_SESSION in a previous PHP page] ; if later in php script displaying $_SESSION, its contents displays as expected. This, I did believe, proves that $_SESSION content has been recorded correctly, without waiting the end of running the php script.

2 - In the next php page... $_SESSION displays without my last changes

Any idea? In which situations do you think this might happen?

3 - More background info: the first page does not call directly the second; in fact, these pages are called one after the other from an html/javascript script running on the client with the iUi library. This runs Ajaxly the php scripts: they run on the server and any html/text output is handled on the client. And yes, this works fine for the php scripts before the ones where I meet the problem.
Avatar of rakjosh
rakjosh
Flag of India image

Hi,
have you included session_start(); on other pages.
Avatar of Loganathan Natarajan
You could always check the session values on the pages, by just printing this line,

print '<pre>';
print_r ($_SESSION);
Avatar of Bernard Savonet

ASKER

@rakjosh: yes I have
@logutdotcom: yes, I do. That's how I see that the values are there "before" and not "after"

B-) just for the record, a nice way of doing this is basically:
echo "** SESSION values: <pre>", print_r($_SESSION,true), "</pre><br>***";
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Ray:

- intended result of session_write_close: as presented in http://www.php.net/manual/en/function.session-write-close.php, this speeds up the recording of session data in server space, and I am assuming that it avoids some of the problems sometimes found when you write $_SESSION data and later on try to reread it: on some configurations it fails, as it is not guaranteed that the session data is recorded until you end the session by leaving the page.

- anyway, I just removed the instruction, and nothing has changed
OK, if that is the case, I think I would be looking for conditional statements that deal with the contents of $_SESSION.  Are you using HTML frames to run multiple concurrent requests that use the same session?  I am curious about the idea of "write $_SESSION data and later on try to reread it" - what does that mean, exactly?  It seems like inside any given request, you would be able to use $_SESSION like any other variable (and in fact you can, even if you have not executed session_start() which to my way of thinking is a terrible design flaw in PHP).  So the part about "write session data" may not be in play until the session is deliberately closed, or the script is terminated, unless you are using HTML frames.  I do not know what would happen to the session data if the script terminated in a fatal error.
Solved: remove ALL session_write_close as suggested hy Ray.
(I had not removed some existing instances, hence my program was still not working).

1 - Although page http://www.php.net/manual/en/function.session-write-close.php might be interpreted as "session_write_close is recommended for improving performance" it should hav a footnote "but do NOT use it in PHP pages called thru Ajax from a javascript page.

2 - Page http://stackoverflow.com/questions/5130241/php-session-write-close-keeps-sending-a-set-cookie-header presents a possible explanation.
Excellent as usual Ray! Thx a lot.
Wow, thanks for your explanation of that.  I never use session_write_close() and so I would never have gotten tripped by an AJAX-related issue!  That's good information to know.

All the best, ~Ray