Jump to content


Photo

Manipulating the $_SESSION array in the __destruct method


  • Please log in to reply
No replies to this topic

#1 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 12 August 2006 - 04:34 AM

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 ()
	{
		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;
		}
	}
}
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?
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users