Jump to content

Manipulating the $_SESSION array in the __destruct method


Buyocat

Recommended Posts

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

public function __construct ()
{
session_name('somename');
session_set_cookie_params(3600);
session_start();
$this->_sdata = array();
foreach ($_SESSION as $key => $value)
{
$this->_sdata[$key] = $value;
}
}

public function __destruct()
{
foreach ($this->_sdata as $key => $value)
{
$_SESSION[$key] = $value;
}
}
}
[/code]
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?
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.