note 99140 deleted from function.session-set-save-handler by danbrown

From: Date: Fri, 30 Jul 2010 16:32:24 +0000
Subject: note 99140 deleted from function.session-set-save-handler by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-171238@lists.php.net to get a copy of this message
Note Submitter: j dot olliero at envisagetheweb dot com ---- Many thanks to newton at tricko dot com dot br for this excellent alternative solution to sessions. I have spent hours struggling with sessions not working properly (the variables were not saved from one page to another on the production server but it worked fine on the development server). Couldn't find where the bug was coming from so in the end I opted for the solutions suggested by newton at tricko dot com dot br and it worked brilliantly. I don't think I will ever bother with classic sessions now that I have this new solution in place. Just one comment : I had a problem with the code in the __destruct method not being executed when I wanted it to and therefore, the data in the session was not being stored in the database. So I copied the code sitting in the__destruct method and I put it in a write() method that I added and I called this write() method in the footer of each page script using the session. I also had to comment out the __destruct method as somehow it was called after the connection to the database had been closed, hence generating an error about failing to connect to the database when trying to store the session data. Here is the code I used (Note that I renamed the database table to 'custom_session' but it is the same table as the one described by newton): <?php class SessionDB { private $data=null; private $session_id=null; private $minutes_to_expire=3600; // TIME TO MAINTAIN DATA ON DB public function __construct(){ global $SESSION; if (isset($_COOKIE['session_id'])){ $this->session_id = $_COOKIE['session_id']; } else { $this->session_id = md5(microtime().rand(1,9999999999999999999999999)); // GENERATE A RANDOM ID setcookie('session_id',$this->session_id); $sql = "INSERT INTO custom_session (session_id, updated_on) VALUES ('{$this->session_id}', NOW())"; mysql_query($sql); } $sql = "SELECT value FROM custom_session WHERE session_id='{$this->session_id}'"; $query = mysql_query($sql); $this->data = unserialize(mysql_result($query, 0, 'value')); $SESSION = $this->data; } private function expire(){ $date_to_delete = date("Y-m-d H:i:s", time()-60*$this->minutes_to_expire); $sql = "DELETE FROM custom_session WHERE update_on <= '$date_to_delete'"; mysql_query($sql); } public function write(){ global $SESSION; $this->data = serialize($SESSION); $sql = "UPDATE custom_session SET value='{$this->data}', updated_on=NOW() WHERE session_id='{$this->session_id}'"; mysql_query($sql); $this->expire(); } //public function __destruct(){ // global $SESSION; // $this->data = serialize($SESSION); // $sql = "UPDATE custom_session SET value='{$this->data}', updated_on=NOW() WHERE session_id='{$this->session_id}'"; // mysql_query($sql); // $this->expire(); // } } ?> In my PHP page header, I set up the session: require_once('SessionDB.php'); $SESSION = null; global $SESSION; $session_db = new SessionDB(); and in my PHP page footer, I make sure the session data is written to the database by calling: $session_db->write(); For the rest, I use it as a normal session variable (e.g. echo $SESSION['firstname'];), making sure that each time I use $SESSION (in the page script or in class methods or in helper functions) I have declared $SESSION as: global $SESSION before attempting to add or retrieve variables from the session so that all my pages are referring to the same $SESSION variable. Hope this will help anyone struggling with sessions. Thanks again to newton!

« previous php.notes (#171238) next »