PHP Session_set_save_handler Save session to MySQL

Source: Internet
Author: User
Tags session id

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;=&GT;&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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.