PHP stores session information to database class instances

Source: Internet
Author: User
This article mainly introduces the class in which PHP stores session information to the database. The instance analyzes the class in which php encapsulated session information is stored in the database, which has some reference value, for more information, see

This article mainly introduces the class in which PHP stores session information to the database. The instance analyzes the class in which php encapsulated session information is stored in the database, which has some reference value, for more information, see

This example describes how PHP stores session information to a database. Share it with you for your reference. The specific analysis is as follows:

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!

The PHP code is as follows:

The Code is as follows:


/**
* Class for storing session information to the database
* 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;
/**
* @ Access private
* @ Var string name of the table where 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: path for saving the session
* @ 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 the 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 the maximum session 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 ();

I hope this article will help you with php programming.

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.