Customization of PHP session processing

Source: Internet
Author: User
Tags php session
user-define-session-inc.php File Code:

Copy the Code code as follows:


function Mysession_open ($save _path, $session _name)
{
@mysql_connect ("localhost", "root", "1981427")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("test")//Select Database MyDB
Or Die ("database does not exist or is not available");
return true;
}
function Mysession_close ()
{
return true;
}
function Mysession_read ($key)
{
@mysql_connect ("localhost", "root", "1981427")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("test")//Select Database MyDB
Or Die ("database does not exist or is not available");
$expiry _time = time (); Get session Expiration Time
Execute SQL statement to get the value of 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")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("test")//Select Database MyDB
Or Die ("database does not exist or is not available");
$expiry _time = time () + 1200; Get session Expiration Time
Query whether the key value of 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, or the update operation is performed
if (mysql_numrows ($query) = = 0)
{
Execute SQL statement Insert value of 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")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("test")//Select Database MyDB
Or Die ("database does not exist or is 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", "1981427")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("test")//Select Database MyDB
Or Die ("database does not exist or is 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 up user-defined session store
Session_set_save_handler (' Mysession_open ',
' Mysession_close ',
' Mysession_read ',
' Mysession_write ',
' Mysession_destroy ',
' mysession_gc ');
?>


Copy the Code code as follows:


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


Copy the Code code as follows:


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

The above describes the PHP session processing of the customization, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.