note 99140 added to function.session-set-save-handler
| From: | j dot olliero at envisagetheweb dot com | Date: | Fri, 30 Jul 2010 01:50:30 +0000 |
| Subject: | note 99140 added to function.session-set-save-handler | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171214@lists.php.net to get a copy of this message | ||
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!
----
Server IP: 69.147.83.197
Probable Submitter: 79.69.186.226
----
Manual Page -- http://www.php.net/manual/en/function.session-set-save-handler.php
Edit -- https://master.php.net/note/edit/99140
Del: integrated -- https://master.php.net/note/delete/99140/integrated
Del: useless -- https://master.php.net/note/delete/99140/useless
Del: bad code -- https://master.php.net/note/delete/99140/bad+code
Del: spam -- https://master.php.net/note/delete/99140/spam
Del: non-english -- https://master.php.net/note/delete/99140/non-english
Del: in docs -- https://master.php.net/note/delete/99140/in+docs
Del: other reasons-- https://master.php.net/note/delete/99140
Reject -- https://master.php.net/note/reject/99140
Search -- https://master.php.net/manage/user-notes.php