How does php save the session to the MySql database?

Source: Internet
Author: User
Tags mysql database

In php, the default session storage method is hard disk, and php can also change the default storage method.
The session_set_save_handler method is used. The following describes how to save the session to the MySql database.

1. Create a session table

The code is as follows: Copy code
Create table 'session '(
'Sessionid' varchar (128) not null,
'Uid' int (11) not null,
'Data' mediumblob not null,
'Timestamp' int (11) not null,
'IP' varchar (15) not null,
Primary key ('sessionid '),
KEY 'time _ session' ('timestamp', 'sessionid ')
) ENGINE = MyISAM default charset = utf8;

Uid is a reserved field

2. Custom session class

The code is as follows: Copy code

<? Php
Class CustomSession {
Private static $ db_host = "localhost ";
Private static $ db_user = "root ";
Private static $ db_password = "";
Private static $ database = "session ";

Private $ conn;

Public static function getInstance (){
Static $ instance = null;
If ($ instance = null ){
$ Instance = new CustomSession ();
  }

Return $ instance;
 }

Public function _ construct (){
Session_set_save_handler (
Array ($ this, "open "),
Array ($ this, "close "),
Array ($ this, "read "),
Array ($ this, "write "),
Array ($ this, "destroy "),
Array ($ this, "gc ")
);
 }

Public function _ destruct (){
Session_write_close ();
 }

Public function open (){
$ This-> conn = mysql_connect (CustomSession: $ db_host, CustomSession: $ db_user, CustomSession: $ db_password );
Mysql_select_db (CustomSession: $ database, $ this-> conn );
 }

Public function close (){

Mysql_close ($ this-> conn );
 }

Public function read ($ id ){
$ Escaped_id = mysql_escape_string ($ id );
$ Res = $ this-> query ("select * from 'session 'where 'sessionid' =' $ escaped_id '");
If ($ row = mysql_fetch_assoc ($ res )){
$ This-> query ("update 'session 'set' timetamp '= UTC_TIMESTAMP () where 'sessionid' =' $ escaped_id '");
Return $ row ['data'];
  }
Return "";
 }

Public function write ($ id, $ data ){
$ Query = "replace into 'Session' ('sessionid', 'data', 'IP', 'timestamp') values ('% s',' % s ', '% s', UNIX_TIMESTAMP (UTC_TIMESTAMP ()))";
$ This-> query (sprintf ($ query, mysql_escape_string ($ id), mysql_escape_string ($ data), $ _ SERVER ["REMOTE_ADDR"]); // www.111cn.net
 }

Public function destroy ($ id ){
$ Escaped_id = mysql_escape_string ($ id );
$ Res = $ this-> query ("delete from 'session 'where 'id' =' $ escaped_id '");
Return (mysql_affected_rows ($ res) = 1 );
 }

Public function gc ($ lifetime ){
$ This-> query ("delete from 'session 'where UNIX_TIMESTAMP (UTC_TIMESTAMP ()-'timestamp'> $ lifetime ");
 }

Public function query ($ query ){
$ Res = mysql_query ($ query, $ this-> conn );
Return $ res;
 }
}

?>

3. Test procedure

The code is as follows: Copy code

<? Php
Include ('./CustomSession. class. Php ');

CustomSession: getInstance ();

Session_start ();

$ _ SESSION ['username'] = 'feng ';

Print_r ($ _ SESSION );

?>

After running the test program, you can view the database and find that the session record has been added to the session table.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.