note 96305 added to function.session-set-save-handler

From: Date: Fri, 19 Feb 2010 16:46:14 +0000
Subject: note 96305 added to function.session-set-save-handler
Groups: php.notes 
Request: Send a blank email to php-notes+get-166178@lists.php.net to get a copy of this message
I have been done with session problems creating a new and my own session system. This is very simple, bellow the class file, a good pratice is include these file as your first line: <? 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 tb_session_db (session_id, updated_on) VALUES ('{$this->session_id}', NOW())"; mysql_query($sql); } $sql = "SELECT value FROM tb_session_db 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 tb_session_db WHERE update_on <= '$date_to_delete'"; mysql_query($sql); } public function __destruct(){ global $SESSION; $this->data = serialize($SESSION); $sql = "UPDATE tb_session_db SET value='{$this->data}', updated_on=NOW() WHERE session_id='{$this->session_id}'"; mysql_query($sql); $this->expire(); } } /* TABLE STRUCTURE CREATE TABLE IF NOT EXISTS tb_session_db ( session_id varchar(32) NOT NULL, value blob, updated_on datetime DEFAULT NULL, PRIMARY KEY (session_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; */ ?> With this file included, connected to MySQL database who has the table tb_session_db I can easy do that into my config file: <? require_once('session_db.php'); define('MYSQL_HOST','mysql.tricko.com.br'); define('MYSQL_USER','.......'); define('MYSQL_PASS','.......'); define('MYSQL_BASE','.......'); mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die('Cannot connect'); mysql_select_db(MYSQL_BASE) or die('Cannot select DB'); mysql_set_charset('utf8_general_ci'); $SESSION = null; global $SESSION; $session_db = new SessionDB(); ?> To use $SESSION on a function/class method, you must call: global $SESSION For who will not work with this over classes or functions, it is a very simple/fast way to work! ---- Server IP: 69.147.83.197 Probable Submitter: 187.47.250.150 ---- Manual Page -- http://www.php.net/manual/en/function.session-set-save-handler.php Edit -- https://master.php.net/note/edit/96305 Del: integrated -- https://master.php.net/note/delete/96305/integrated Del: useless -- https://master.php.net/note/delete/96305/useless Del: bad code -- https://master.php.net/note/delete/96305/bad+code Del: spam -- https://master.php.net/note/delete/96305/spam Del: non-english -- https://master.php.net/note/delete/96305/non-english Del: in docs -- https://master.php.net/note/delete/96305/in+docs Del: other reasons-- https://master.php.net/note/delete/96305 Reject -- https://master.php.net/note/reject/96305 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166178) next »