Jump to content


This topic is now archived and is closed to further replies.


Manipulating the $_SESSION array in the __destruct method

Recommended Posts

I wanted to store the session data in an object, so I created a class that looks more or less like this:
class Session_Container
public $_sdata;

public function __construct ()
$this->_sdata = array();
foreach ($_SESSION as $key => $value)
$this->_sdata[$key] = $value;

public function __destruct()
foreach ($this->_sdata as $key => $value)
$_SESSION[$key] = $value;
I think it is pretty clear what the above code is meant to do; as a side note this code works successfully on my local machine.  However on my webserver the session data fails to stick from page to page.  I have tried echoing things and checking for sessions in teh destructor and everything seems to be set.  To make things more troublesome if I convert the above into a simple script that performs the same function the session data does stick.  Lastly, if I move the second loop out of the destructor and into the constructor or any other method then the session data sticks as well.  So, it seems like the destructor is the culprit only I'm not exactly sure why.  The session appears to be set right before the foreach loop in the method and the sdata array is populated.  Does anyone have any ideas on how to get around this issue?

Share this post

Link to post
Share on other sites


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.