Save session to MySQL
<?php/** * SessionMysql Database storage class */defined (' In_qian ') or exit (' access Denied ');class sessionmysql {public $lifetime = 1800; // expiry date, Unit: SEC (s), Default 30 minutes public $db;p ublic $table;/** * Constructor */public function __construct () {session_module_name ("user");//ini_set (' Session.save_handler ', ' user '); ini_set (' session.use_cookies ', 1); $this->db = base::loadmodel (' Sessionmodel '); $this->lifetime = base::loadconfig (' System ', ' session_lifetime '); session_set_ Save_handler (Array (& $this, ' open '),// performs an array (& $this, ' close ') when running Session_Start (),// is executed when script execution completes or calls Session_write_close () or session_destroy (), that is, after all sessions have been executed, an array ( & $this, ' read '),// executes when running Session_Start () because at Session_Start, it goes to the current session data array (& $this, ' write '),// This method at the end of the script and using SessiOn_write_close () forces the session data to be executed when the array (& $this, ' Destroy '),// is run when Session_destroy () executes the array (&$ this, ' GC ')// execution probabilities are determined by the values of session.gc_probability and session.gc_divisor, and the time is after open,read, session _start will execute Open,read and GC successively); Session_Start (); // This is also necessary, open session, must be executed session_set_save_handler behind}/** * session_set_save_handler open methods * * @param $savePath * @param $sessionName * @return true */public function open ($savePath, $ sessionname) {return true;} /** * session_set_save_handler close Methods * * @return bool */public Function close () {return $this->gc ($this->lifetime);} /** * Read Session_id * * session_set_save_handler read method * @return string read Session_id */public function read ($sessionId) {$condition = array ( ' WHERE ' &NBSP;=>&NBsp;array (' session_id ' => $sessionId), ' Fields ' => ' data '), $row = $this- >db->fetchfirst ($condition);return $row ? $row [' Data '] : ';} /** * Write session_id value * * @param $sessionId session id * @param $data values * @return mixed query execution Results */public function write ( $sessionId, $data) {$userId = isset ($_session[' userid ") ? $_session[' userid '] : 0; $roleId = isset ($_session[' Roleid ') ? $_session[' Roleid '] : 0;$ Grouid = isset ($_session[' grouid ') ? $_session[' grouid '] : 0; $m = Defined (' route_m ') ? ROUTE_M : '; $c = defined (' Route_c ') ? route_c : '; $a = defined (' route_a ') ? ROUTE_A : ';if (strlen ($data) > 255) {$data = '‘;} $ip = get_ip (); $sessionData = array (' session_id ' => $sessionId, ' user_id ' => $userId, ' IP ' => $ip, ' last_visit ' => sys_time, ' role_id ' => $roleId, ' group_id ' => $ Grouid, ' m ' => $m, ' C ' => $c, ' a ' => $a, ' data ' => $data,;return $this->db- >insert ($sessionData, 1, 1);} /** * Delete the specified session_id * * @param string $sessionId Session id * @ Return bool */public function destroy ($sessionId) {return $this->db-> Delete (Array (' session_id ' => $sessionId));} /** * Delete expired session * * @param $lifetime session validity period (in seconds) * @return &NBSP;BOOL*/PUBLIC&NBSP;FUNCTION&NBSP;GC ($lifetime) {$expireTime = SYS_TIME - $lifetime;return $this->db->delete ("' Last_visit ' < $expireTime");}}? >
PHP Session_set_save_handler Save session to MySQL