A detailed introduction to the PHP session storage method _php tips

Source: Internet
Author: User
Tags session id php session
PHP provides three ways for sesion storage: File/memory/custom storage, using file storage by default. This is not a good way to go on a Web site with a large volume of traffic, because it can result in a lot of redundancy in the input and output.
Here is a MySQL database based session storage method.
the first configuration to do is as follows:
1>php.ini Session.save_handler = files in the files to user, other default can, restart Apache (seems to have not changed the line AH)
2> The database created by this instance is called PHP, Username:root password:root
The table structure of the database designed by this example looks like this:
Copy Code code as follows:

CREATE TABLE MySession (
Session_key char (not NULL),
Session_data text,
Session_expiry Int (11),
Primary KEY (Session_key)
);

The first column represents the storage session ID, the second column stores the data in session, and the third column stores the expiration date, hehe (table structure is so simple)
Here is the key to the implementation of the custom function Session_set_save_handler (...)
Copy Code code as follows:

<?php
function Mysession_open ($save _path, $session _name)
{
@mysql_connect ("localhost", "root", "root")//Select database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("PHP")//Select Database MyDB
Or Die ("database does not exist or not available");
return true;
}
function Mysession_close ()
{
return true;
}
function Mysession_read ($key)
{
@mysql_connect ("localhost", "root", "root")//Select database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("PHP")//Select Database MyDB
Or Die ("database does not exist or not available");
$expiry _time = time (); Get session Expiration Time
Execute SQL statement to get the value of the session
$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")//Select database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("PHP")//Select Database MyDB
Or Die ("database does not exist or not available");
$expiry _time = time () + 1200; Get session Expiration Time
Whether the key value of the query session already exists
$query = @mysql_query ("Select Session_data from MySession"
." where Session_key = ' $key ')
Or Die ("SQL statement execution failed");
If it does not exist, the insert operation is performed, otherwise the update operation is performed
if (mysql_numrows ($query) = = 0)
{
Executes the value of the SQL statement insert session
$query = @mysql_query ("INSERT into mysession values (' $key ', ' $data ', $expiry _time)")
Or Die ("SQL statement execution failed");
}
Else
{
Execute SQL statement to update 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")//Select database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("PHP")//Select Database MyDB
Or Die ("database does not exist or not available");
Execute SQL statement Delete 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")//Select database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("PHP")//Select Database MyDB
Or Die ("database does not exist or not available");
$expiry _time = time ();
Execute SQL statement Delete session
$query = @mysql_query ("Delete from mysession where Session_expiry < $expiry _time")
Or Die ("SQL statement execution failed");
return $query;
}
Set User Custom session Store
Session_set_save_handler (' Mysession_open ',
' Mysession_close ',
' Mysession_read ',
' Mysession_write ',
' Mysession_destroy ',
' mysession_gc ');
?>

The last side is the test code.
1> Storage Page save.php
Copy Code code as follows:

<?php
Include (' user-define-session-inc.php '); Files that contain session_set_save_handler definitions
Session_Start ();
$_session[' username ' = "Simon";
$_session[' password '] = "123456";
?>

2. Display Page show.php
Copy Code code as follows:

<?php
Include (' user-define-session-inc.php '); Files that contain session_set_save_handler definitions
Session_Start ();
echo "UserName:". $_session[' UserName '. " <BR> ";
echo "PassWord:". $_session[' PassWord '. " <BR> ";
?>

Attach a small experience: a day inexplicable again store session when found page report this error
Failed to initialize storage module
Then I found the relevant information to know is my session storage using database file storage, and Session_Start (), my database has not woken up ...

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.