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

From: Date: Sun, 09 Dec 2007 14:51:14 +0000
Subject: note 79706 added to function.session-set-save-handler
Groups: php.notes 
Request: Send a blank email to php-notes+get-133041@lists.php.net to get a copy of this message
blow example and ta summary of these comments. and using the simple native functions of mysql. <?php class Session { /** * a database connection resource * @var resource */ private $_sess_db; /** * Open the session * @return bool */ public function open() { if ($this->_sess_db = mysql_connect(SESSION_DB_HOST, SESSION_DB_USER, SESSION_DB_PASS)) { return mysql_select_db(SESSION_DB_DATABASE, $this->_sess_db); } return false; } /** * Close the session * @return bool */ public function close() { return mysql_close($this->_sess_db); } /** * Close the session * @return bool */ public function close() { return mysql_close($this->_sess_db); } /** * Read the session * @param int session id * @return string string of the sessoin */ public function read($id) { $id = mysql_real_escape_string($id); $sql = sprintf("SELECT data FROM sessions " . "WHERE id = '%s'", $id); if ($result = mysql_query($sql, $this->_sess_db)) { if (mysql_num_rows($result)) { $record = mysql_fetch_assoc($result); return $record['data']; } } return ''; } /** * Write the session * @param int session id * @param string data of the session */ public function write($id, $data) { $sql = sprintf("REPLACE INTO sessions VALUES('%s', '%s', '%s')", mysql_real_escape_string($id), mysql_real_escape_string($data), mysql_real_escape_string(time())); return mysql_query($sql, $this->_sess_db); } /** * Destoroy the session * @param int session id * @return bool */ public function destroy($id) { $sql = sprintf("DELETE FROM sessions WHERE id = '%s'", $id); return mysql_query($sql, $this->_sess_db); } /** * Garbage Collector * @param int life time (sec.) * @return bool * @see session.gc_divisor 100 * @see session.gc_maxlifetime 1440 * @see session.gc_probability 1 * @usage execution rate 1/100 * (session.gc_probability/session.gc_divisor) */ public function gc($max) { $sql = sprintf("DELETE FROM sessions WHERE timestamp < '%s'", mysql_real_escape_string(time() - $max)); return mysql_query($sql, $this->_sess_db); } } //ini_set('session.gc_probability', 50); ini_set('session.save_handler', 'user'); $session = new Session(); session_set_save_handler(array($session, 'open'), array($session, 'close'), array($session, 'read'), array($session, 'write'), array($session, 'destroy'), array($session, 'gc')); // below sample main session_start(); session_regenerate_id(true); if (isset($_SESSION['counter'])) { $_SESSION['counter']++; } else { $_SESSION['counter'] = 1; } ?> ---- Server IP: 219.94.145.73 Probable Submitter: 61.23.71.124 ---- Manual Page -- http://www.php.net/manual/en/function.session-set-save-handler.php Edit -- https://master.php.net/note/edit/79706 Del: integrated -- https://master.php.net/note/delete/79706/integrated Del: useless -- https://master.php.net/note/delete/79706/useless Del: bad code -- https://master.php.net/note/delete/79706/bad+code Del: spam -- https://master.php.net/note/delete/79706/spam Del: non-english -- https://master.php.net/note/delete/79706/non-english Del: in docs -- https://master.php.net/note/delete/79706/in+docs Del: other reasons-- https://master.php.net/note/delete/79706 Reject -- https://master.php.net/note/reject/79706 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#133041) next »