Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem with PHP SESSION variables changes

Posted on 2012-03-14
9
Medium Priority
?
321 Views
Last Modified: 2012-03-15
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.
0
Comment
Question by:fibo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 1

Expert Comment

by:rakjosh
ID: 37719161
Hi,
have you included session_start(); on other pages.
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 37719219
You could always check the session values on the pages, by just printing this line,

print '<pre>';
print_r ($_SESSION);
0
 
LVL 29

Author Comment

by:fibo
ID: 37719336
@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>***";
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37719515
What is the intended result of calling session_write_close()?  Why not just wait and let the garbage collector handle the cleanup?
0
 
LVL 29

Author Comment

by:fibo
ID: 37719744
@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
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37719800
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.
0
 
LVL 29

Author Comment

by:fibo
ID: 37727035
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.
0
 
LVL 29

Author Closing Comment

by:fibo
ID: 37727053
Excellent as usual Ray! Thx a lot.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37727324
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question