Home > Article > Backend Development > Customization of php session processing
user-define-session-inc.php file code:
Copy code The code is as follows:
function mysession_open($save_path, $session_name)
{
@mysql_connect("localhost", "root","1981427") //You need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db("test") //Select database mydb
or die("Database failed exists or is not available");
return true;
}
function mysession_close()
{
return true;
}
function mysession_read($key)
{
@mysql_connect("localhost", "root","1981427" ) //You need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db("test") //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","1981427") //You need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db ("test") //Select database mydb
or die("Database does not exist or is unavailable");
$expiry_time = time() + 1200; //Get the Session expiration time
//Query whether the Session key value has expired Exists
$query = @mysql_query("select session_data from mysession "
."where session_key = '$key'")
or die("SQL statement execution failed");
//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 statement 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","1981427") //You need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db("test") / /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","1981427") //You need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db("test") //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 customization Session storage
session_set_save_handler('mysession_open',
'mysession_close',
'mysession_read',
'mysession_write',
'mysession_destroy',
'mysession_gc');
?>
Copy code Code As follows:
include('user-define-session-inc.php'); //File containing session_set_save_handler definition
session_start();
$_SESSION['username'] = "zhuzhao";
$_SESSION['password'] = "123456";
?> '); //File containing session_set_save_handler definition
session_start();
?> The above introduces the customization of PHP session processing, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.