Phpsession processing customization _ PHP Tutorial

Source: Internet
Author: User
Phpsession processing customization. User-define-session-inc.php File Code: Copy the code as follows :? Phpfunctionmysession_open ($ save_path, $ session_name) {@ mysql_connect (localhost, root, 1981427) select the number of user-define-session-inc.php file codes:

The code is as follows:


Function mysession_open ($ save_path, $ session_name)
{
@ Mysql_connect ("localhost", "root", "1981427") // you must connect to the database server before selecting a 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 ");
Return true;
}

Function mysession_close ()
{
Return true;
}

Function mysession_read ($ key)
{
@ Mysql_connect ("localhost", "root", "1981427") // you must connect to the database server before selecting a 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 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", "1981427") // you must connect to the database server before selecting a 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 () + 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", "1981427") // you must connect to the database server before selecting a 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 ");
// 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", "1981427") // you must connect to the database server before selecting a 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 ();
// 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 code is as follows:


Include ('User-define-session-inc.php '); // contains the file defined by session_set_save_handler

Session_start ();
$ _ SESSION ['username'] = "zhuzhao ";
$ _ SESSION ['password'] = "123456 ";
?>


The code is as follows:


Include ('User-define-session-inc.php '); // contains the file defined by session_set_save_handler

Session_start ();
Echo "UserName:". $ _ SESSION ['username']."
";
Echo "PassWord:". $ _ SESSION ['password']."
";
?>

The pipeline code is as follows :? Php function mysession_open ($ save_path, $ session_name) {@ mysql_connect ("localhost", "root", "1981427") // number of options...

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.