List Info

Thread: note 78367 added to function.session-set-save-handler




note 78367 added to function.session-set-save-handler
user name
2007-10-08 11:36:34
After much messing around to get php to store session in a
database and reading all these notes, I come up with this
revised code based on 'stalker at ruun dot de' class.
I wanted to use PEAR::MDB2. The only assumption I make is
your PEAR::MDB2 object is called $db

SQL:
CREATE TABLE `sessions` (
  `session_id` int(10) unsigned NOT NULL auto_increment,
  `session` varchar(255) character set utf8 collate utf8_bin
NOT NULL,
  `session_expires` int(10) unsigned NOT NULL default '0',
  `session_data` mediumtext collate utf8_unicode_ci,
  PRIMARY KEY  (`session_id`),
  KEY `session` (`session`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci

<?php
class Session {
	// session-lifetime
	public $lifeTime;
	function __construct ($db) {
		// get session-lifetime
		$this->lifeTime =
get_cfg_var("session.gc_maxlifetime");
       	// open database-connection
		$this->mdb2 =& MDB2::factory($db);
		if (PEAR::isError($this->mdb2)) {
			$php_errormsg .= $this->mdb2->getMessage();
			$php_errormsg .= $this->mdb2->getDebugInfo();
		}
		session_set_save_handler(array(&$this, 'open'),
								array(&$this, 'close'),
								array(&$this, 'read'),
								array(&$this, 'write'),
								array(&$this, 'destroy'),
								array(&$this, 'gc'));
		register_shutdown_function('session_write_close');
		session_start();
	   	return true;
	}
	function open($savePath, $sessName) {
		// get session-lifetime
		$this->lifeTime =
get_cfg_var("session.gc_maxlifetime");
		return true;
	}
	function close() {
		$this->gc(ini_get('session.gc_maxlifetime'));
		// close database-connection
		return $this->mdb2->disconnect();
	}
	function read($sessID) {
		global $php_errormsg;
		// fetch session-data
		$query = "
			SELECT session_data FROM sessions
			WHERE session = '$sessID'
			AND session_expires > 
		".time();
		$result = $this->mdb2->queryOne($query);
		// return data or an empty string at failure
		if (MDB2::isError($result)) {
			$php_errormsg .= $result->getMessage();
			$php_errormsg .= $result->getDebugInfo ();
			return false;
		}
		return $result;
	}
	function write($sessID,$sessData) {
		global $php_errormsg;
		// new session-expire-time
		$newExp = time() + $this->lifeTime;
		// is a session with this id in the database?
		$query = "
			SELECT * FROM sessions
			WHERE session = '$sessID'
		";
		$result = $this->mdb2->query($query);
		// if yes,
  		if($result->numRows()) {
			// ...update session-data
			$query = "
				UPDATE sessions
				SET session_expires = '$newExp',
	 			session_data = '$sessData'
				WHERE session = '$sessID'
			";
  		}
		// if no session-data was found,
  		else {
			// create a new row
			$query = "
				INSERT INTO sessions (
	 				session,
  					session_expires,
  					session_data)
				VALUES(
	 				'$sessID',
  					'$newExp',
  					'$sessData')
			";
  		}
		$result = $this->mdb2->exec($query);
		// if something happened, return true
		if (MDB2::isError($result)) {
			$php_errormsg .= $result->getMessage();
			$php_errormsg .= $result->getDebugInfo ();
			return false;
		} else { 
			// ...else return true
			return true;
		}
	}
	function destroy($sessID) {
		global $php_errormsg;
		// delete session-data
		$query = "
			DELETE FROM sessions
			WHERE session = '$sessID'
		";
		$result = $this->mdb2->exec($query);
		// if session was not deleted, return false,
 		if (MDB2::isError($result)) {
			$php_errormsg .= $result->getMessage();
			$php_errormsg .= $result->getDebugInfo ();
			return false;
 		} else { 
			// ...else return true
			return true;
		}
	}
	function gc($sessMaxLifeTime) {
		global $php_errormsg;
		// delete old sessions
		$query = "
			DELETE FROM sessions
			WHERE session_expires < 
		".time();
		$result = $this->mdb2->exec($query);
		// return affected rows
		if (MDB2::isError($result)) {
			$php_errormsg .= $result->getMessage();
			$php_errormsg .= $result->getDebugInfo ();
		}
		return $result;
	}
}
?>
----
Server IP: 194.145.210.4
Probable Submitter: 212.139.73.173
----
Manual Page -- http://www.php.net/manual/en/function.session-
set-save-handler.php
Edit        -- https://master
.php.net/note/edit/78367
Del: integrated  -- h
ttps://master.php.net/note/delete/78367/integrated
Del: useless     -- http
s://master.php.net/note/delete/78367/useless
Del: bad code    -- htt
ps://master.php.net/note/delete/78367/bad+code
Del: spam        -- https:/
/master.php.net/note/delete/78367/spam
Del: non-english -- 
https://master.php.net/note/delete/78367/non-english
Del: in docs     -- http
s://master.php.net/note/delete/78367/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78367
Reject      -- https://mast
er.php.net/note/reject/78367
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )