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 not present, the insert operation is performed, otherwise the update operation is performed
if (mysql_numrows ($query) = 0)
{
//Execute SQL statement inserts the value of the session
$query = @mysql_query ("INSERT into mysession values (' $key ', ' $data ', $expiry _time)")
or Die ("SQL statement execution failed");
}
Else
{
Execute SQL Statement 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 '); File containing the Session_set_save_handler definition
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 '); File containing the Session_set_save_handler definition
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 ...