A detailed introduction to the PHP session Storage Method

Source: Internet
Author: User
Tags php session

PHP provides three methods for sesion storage: File/memory/custom storage. By default, file storage is used.This method is not suitable for websites with a large access volume, because it will lead to a large number of input and output redundancy.
The following describes a session Storage Method Based on the Mysql database.
The first configuration is as follows:
1> change the files in session. save_handler = files to User in php. ini. Restart Apache by default)
2> the name of the database created in this instance is php, username: root password: root.
The table structure of the database designed for this instance is as follows: Copy codeThe Code is as follows: create table mysession (
Session_key char (32) not null,
Session_data text,
Session_expiry int (11 ),
Primary key (session_key)
);

The first column indicates the session ID, the second column stores the data in the session, and the third column stores the validity period. (The table structure is so simple)
The following is the key custom function implementation session_set_save_handler (......)Copy codeThe Code is as follows: <? Php
Function mysession_open ($ save_path, $ session_name)
{
@ Mysql_connect ("localhost", "root", "root") // You must connect to the database server before selecting a database.
Or die ("database server connection failed ");
@ Mysql_select_db ("php") // select the database mydb
Or die ("the database does not exist or is unavailable ");
Return true;
}
Function mysession_close ()
{
Return true;
}
Function mysession_read ($ key)
{
@ Mysql_connect ("localhost", "root", "root") // You must connect to the database server before selecting a database.
Or die ("database server connection failed ");
@ Mysql_select_db ("php") // select the database mydb
Or die ("the database does not exist or is unavailable ");
$ Expiry_time = time (); // get the Session expiration time
// Execute an SQL statement to obtain the Session Value
$ Query = @ mysql_query ("select session_data from mysession"
. "Where session_key = '$ key' and session_expiry> $ expiry_time ")
Or die ("SQL statement execution failed ");
If ($ row = mysql_fetch_array ($ query ))
Return $ row ['session _ data'];
Else
Return false;
}
Function mysession_write ($ key, $ data)
{
@ Mysql_connect ("localhost", "root", "root") // You must connect to the database server before selecting a database.
Or die ("database server connection failed ");
@ Mysql_select_db ("php") // select the database mydb
Or die ("the database does not exist or is unavailable ");
$ Expiry_time = time () + 1200; // get the Session expiration time
// Query whether the Session key value already exists
$ Query = @ mysql_query ("select session_data from mysession"
. "Where session_key = '$ key '")
Or die ("SQL statement execution failed ");
// If the data does not exist, insert the data; otherwise, update the data.
If (mysql_numrows ($ query) = 0)
{
// Execute the SQL statement to insert the Session Value
$ Query = @ mysql_query ("insert into mysession values ('$ key',' $ data', $ expiry_time )")
Or die ("SQL statement execution failed ");
}
Else
{
// Execute an SQL statement to update the Session Value
$ Query = @ mysql_query ("update mysession set"
. "Session_data = '$ data', session_expiry = $ expiry_time"
. "Where session_key = '$ key '")
Or die ("SQL statement execution failed ");
}
Return $ query;
}
Function mysession_destroy ($ key)
{
@ Mysql_connect ("localhost", "root", "root") // You must connect to the database server before selecting a database.
Or die ("database server connection failed ");
@ Mysql_select_db ("php") // select the database mydb
Or die ("the database does not exist or is unavailable ");
// Execute the SQL statement to delete the Session
$ Query = @ mysql_query ("delete from mysession where session_key = '$ key '")
Or die ("SQL statement execution failed ");
Return $ query;
}
Function mysession_gc ($ expiry_time)
{
@ Mysql_connect ("localhost", "root", "root") // You must connect to the database server before selecting a database.
Or die ("database server connection failed ");
@ Mysql_select_db ("php") // select the database mydb
Or die ("the database does not exist or is unavailable ");
$ Expiry_time = time ();
// Execute the SQL statement to delete the Session
$ Query = @ mysql_query ("delete from mysession where session_expiry <$ expiry_time ")
Or die ("SQL statement execution failed ");
Return $ query;
}
// Set custom Session Storage
Session_set_save_handler ('mysession _ open ',
'Mysession _ close ',
'Mysession _ read ',
'Mysession _ write ',
'Mysession _ destroy ',
'Mysession _ gc ');
?>

The last part is the test code.
1> save. php
Copy codeThe Code is as follows: <? Php
Include ('user-define-session-inc.php '); // contains the file defined by session_set_save_handler
Session_start ();
$ _ SESSION ['username'] = "Simon ";
$ _ SESSION ['Password'] = "123456 ";
?>

2. show. php
Copy codeThe Code is as follows: <? Php
Include ('user-define-session-inc.php '); // contains the file defined by session_set_save_handler
Session_start ();
Echo "UserName:". $ _ SESSION ['username']. "<BR> ";
Echo "PassWord:". $ _ SESSION ['Password']. "<BR> ";
?>

Append a small experience: this error is reported on the page when the session is stored inexplicably.
Failed to initialize storage module
Later, I found out that my session storage uses database file storage, while session_start () didn't wake up in my database...

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.