How does php Save the session to the MySql database?-PHP source code

Source: Internet
Author: User
Most of the sessions are stored on the server, but today we have a function that is to save the session in the database, so that we can perform multiple browsing with ie, let's take a look at the specific implementation example. Most of the sessions are stored on the server, but today we have a function that is to save the session in the database, so that we can perform multiple browsing with ie, let's take a look at the specific implementation example.

Script ec (2); script

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:
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:

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:

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.

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.