Jump to content

Why wont the session write function run?


Jak

Recommended Posts

Hi,
I’m using a custom session handling class in my application, however I’m having trouble with the session write method. I’m using __destruct methods in a couple of classes to modify values in the SESSION variable, but some (not all) of the changes I make are not being saved.

At first I thought it was because PHP was running the session handlers write function before the __destruct methods in my classes, however I have since discovered that its not running the write method at all (sometimes).

I added a line into each of the relevant functions to log what was happening to a file, this is what I got:

[code]** NEW REQUEST **
SESSION CONSTRUCTOR
SESSION READ
FORM DESTRUCTOR

** NEW REQUEST **
SESSION CONSTRUCTOR
SESSION READ
VALIDATION DESTRUCTOR
SESSION WRITE

** NEW REQUEST **
SESSION CONSTRUCTOR
SESSION READ
FORM DESTRUCTOR[/code]

So on the first request (the initial page load) the session is being created, a form is created on the page, and the forms destructor is run.

The second request is the post to the server from the form. The session is fine, the validation classes destructor is run (which writes validation data to the session variable) and then the session write function is run, ALL GOOD.

On this occasion the form failed validation so the third request is the redirect back to the original form. The session is fine, the form destructor is fine (which removes the validation data that the validation class added, as this has been used earlier in the form class) however for some reason the session write method has NOT been run.

Does anyone have any idea why this would happen? I’m completely confused, I assumed the write function would run at the end of every request, and it certainly should if the session data has changed.

Thanks in advance,
Jack
Link to comment
Share on other sites

  • 4 years later...

This thread is four years old, but it comes up on Google.  Here is something that might be related for future people who come across this:

 

http://bugs.php.net/29032

 

Basically $_SESSION is destroyed/unwritable/unpredictable/crap when called in destructors (which could be after execution has finished).  Also, the bug report is 7 years old, so may not be the case at all.  All I can say is that for me, calling $object->__destruct(), where $object is a singleton, works fine, but is not a very neat way of doing it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.