PHP provides three ways to store sessions: File/Memory/Custom storage, the default is to use file storage.It is not convenient to use this method on websites with high traffic. This is very suitable, because this will lead to a large amount of input and output redundancy.
The following introduces a session storage method based on the Mysql database.
The first configuration to be performed is as follows:
1>In php.ini, change the files in session.save_handler = files to User, and other defaults are sufficient. Restart Apache (it seems to be OK without changing it)
2>This example creates The database name is php, username: root password:root
The table structure of the database designed in this example is as follows:
Copy code The 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 stores the session ID, the second column stores the data in the session, and the third column stores the validity period, haha (the table structure is that simple)
The following is The key custom function implementation session_set_save_handler(...)
Copy code The code is as follows:
function mysession_open($save_path, $session_name)
{
@mysql_connect("localhost", "root", "root") //Needed before selecting the database Connect to the database server first
or die("Database server connection failed");
@mysql_select_db("php") //Select database mydb
or die("Database does not exist or is unavailable");
return true;
}
function mysession_close()
{
return true;
}
function mysession_read($key)
{
@mysql_connect(" localhost", "root", "root") //You need to connect to the database server before selecting the 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 the 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") //You need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db("php" ) //Select the database mydb
or die("Database does not exist or is unavailable");
$expiry_time = time() + 1200; //Get the Session expiration time
//Query the key value of the Session Does it already exist? /If it does not exist, perform the insert operation, otherwise perform the update operation
if(mysql_numrows($query) == 0)
{
//Execute the SQL statement to insert 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 statements to update the value of Session
$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 need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db("php" ) //Select database mydb
or die("Database does not exist or is unavailable");
//Execute SQL statement to 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") //You need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db("php") //Select database mydb
or die("Database does not exist or is unavailable");
$expiry_time = time();
//Execute SQL statement to delete Session
$query = @mysql_query("delete from mysession where session_expiry < $expiry_time")
or die("SQL statement execution failed");
return $query;
}
//Set user-defined 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 page save.php
Copy code The code is as follows:include('user-define-session-inc.php'); //File containing session_set_save_handler definition
session_start();
$_SESSION['username' ] = "Simon";
$_SESSION['password'] = "123456";
?>
2. Display page show.php
Copy code The code is as follows:include('user-define-session-inc.php '); //File containing session_set_save_handler definition
session_start();
echo "UserName:".$_SESSION['username']."
";
echo "PassWord:". $_SESSION['password']."
";
?>
Additional little experience: One day when I was trying to store the session again, I found this error on the page
Failed to initialize storage module
After looking for relevant information, I found out that my session storage uses database file storage. , and when session_start(), my database has not woken up yet...
http://www.bkjia.com/PHPjc/327846.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327846.htmlTechArticlePHP provides three ways to store sessions: file/memory/custom storage, the default is to use file storage . It is not appropriate to use this method on a website with a large number of visits, because it will lead to...