Class for storing PHPsession information to the database

Source: Internet
Author: User
Please advise! SessionHandlerInterface is a built-in PHP interface. You can directly implement it. For more information, see the description of session_set_save_handler in the php manual! PHP *** session information is stored in the database class * Table Structure: * CREATETABLEIFNOTEXISTS 'sessioninfo' (* 'sid 'varchar (255) NOT

Please advise! SessionHandlerInterface is a built-in PHP interface. You can directly implement it. For more information, see the description of session_set_save_handler in the php manual! PHP/*** session information is stored in the database class * TABLE Structure: * create table if not exists 'sessioninfo' (* 'sid 'varchar (255) NOT

Please advise!
SessionHandlerInterface is a built-in PHP interface, which can be directly implemented.
For more information, see the explanation of the session_set_save_handler function in the php manual! PHP
/*** Session information is stored in the database class * TABLE Structure: * create table if not exists 'sessioninfo' (* 'sid 'varchar (255) not null, * 'value' text not null, * 'expiration' timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, * primary key ('sid ') *) ENGINE = InnoDB default charset = utf8; */class MySessionHandler implements SessionHandlerInterface {/*** @ access private * @ var object database connection */private $ _ dbLink;/*** @ ac Cess private * @ var string name of the table in which the session is saved */Private $ _ sessionTable;/*** @ access private * @ var string session name */private $ _ sessionName; /*** @ const expiration time */const SESSION_EXPIRE = 10; public function _ construct ($ dbLink, $ sessionTable) {if (! Is_object ($ dbLink) {return false;} $ this-> _ dbLink = $ dbLink; $ this-> _ sessionTable = $ sessionTable ;} /*** open * @ access public * @ param string $ session_save_path to save the session path * @ param string $ session_name session name * @ return integer */public function open ($ session_save_path, $ session_name) {$ this-> _ sessionName = $ session_name; return 0;}/*** close * @ access public * @ return integer */public function close () {return 0;}/*** close session * @ access public * @ param string $ session_id session ID * @ return string */public function read ($ session_id) {$ query = "SELECT value FROM {$ this-> _ sessionTable} WHERE sid = {$ session_id} AND UNIX_TIMESTAMP (expiration) + ". self: SESSION_EXPIRE. "> UNIX_TIMESTAMP (NOW ()"; $ result = $ this-> _ dbLink-> query ($ query); if (! Isset ($ value) | empty ($ value) {$ value = ""; return $ value ;} $ this-> _ dbLink-> query ("UPDATE {$ this-> _ sessionTable} SET expiration = CURRENT_TIMESTAMP () WHERE sid = {$ session_id }"); $ value = $ result-> fetch_array (); $ result-> free (); return $ value ['value'];} /*** write session ** @ access public * @ param string $ session_id session ID * @ param string $ session_data session data * @ return integer */public function write ($ Session_id, $ session_data) {$ query = "SELECT value FROM {$ this-> _ sessionTable} WHERE sid = '{$ session_id}' AND UNIX_TIMESTAMP (expiration) + ". self: SESSION_EXPIRE. "> UNIX_TIMESTAMP (NOW ()"; $ result = $ this-> _ dbLink-> query ($ query); $ result = $ result-> fetch_array (); if (! Empty ($ result )) {$ result = $ this-> _ dbLink-> query ("UPDATE {$ this-> _ sessionTable} SET value = {$ session_data} WHERE sid = {$ session_id }") ;} else {$ result = $ this-> _ dbLink-> query ("insert into {$ this-> _ sessionTable} (sid, value) VALUES ('{$ session_id }', '{$ session_data}') ");} if ($ result) {return 0;} else {return 1 ;}} /*** ecstasy session * @ access public * @ param string $ session_id session ID * @ return integer */public function destroy ($ session_id) {$ result = $ this-> _ dbLink-> query ("delete from {$ this-> _ sessionTable} WHERE sid = '{$ session_id }'"); if ($ result) {return 0;} else {return 1 ;}} /*** garbage collection ** @ access public * @ param string $ maxlifetime session maximum survival time * @ return integer */public function gc ($ maxlifetime) {$ result = $ this-> _ dbLink-> query ("delete from {$ this-> _ sessionTable} WHERE UNIX_TIMESTAMP (expiration) <UNIX_TIMESTAMP (NOW ())-". self: SESSION_EXPIRE); if ($ result) {return 0 ;}else {return 1 ;}}} /*************************************** **************************************** **************************************** * *********/$ dbLink = new mysqli ("localhost ", "root", "root", "test"); $ sessionTable = "sessioninfo"; $ handler = new MySessionHandler ($ dbLink, $ sessionTable); session_set_save_handler ($ handler ); session_start (); $ _ SESSION ['name'] = "test"; echo $ _ SESSION ["name"]; // session_destroy ();
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.