mysql_php tutorial on using the Session_set_save_handler function to overload session storage

Source: Internet
Author: User

$DB _server = "SERVER"; /* Database server hostname */
$DB _name = "dbname"; /* Database name */
$DB _user = "root"; /* Database user */
$DB _pass = "*************"; /* Database Password */

$DB _select_db = "";
$SESS _life = Get_cfg_var ("Session.gc_maxlifetime");

function Sess_open ($save _path, $session _name) {
Global $DB _server, $DB _name, $DB _user, $DB _pass, $DB _select_db;

if (! $DB _select_db = mysql_pconnect ($DB _server, $DB _user, $DB _pass)) {
echo "sorry! MYSQL error:cant Connect to $DB _server as $DB _user ";
echo "MySQL Error:", mysql_error ();
Die
}

if (! mysql_select_db ($DB _name, $DB _select_db)) {
echo "sorry! MYSQL error:unable to select database $DB _name ";
Die
}

return true;
}

function Sess_close () {
return true;
}

function Sess_read ($SessionKey) {
Global $DB _select_db, $SESS _life;
$Query = "Select Sessionarray from cdb_global_sessions WHERE sessionkey =". $SessionKey. " and Sessionexptime >. " Time ();
$Result = mysql_query ($Query, $DB _select_db);

if (list ($SessionArray) = Mysql_fetch_row ($Result)) {
return $SessionArray;
}

return false;
}

function Sess_write ($SessionKey, $VArray) {
Global $DB _select_db, $SESS _life;

$SessionExpTime = time () + $SESS _life;
$SessionArray = Addslashes ($VArray);

$Query = "INSERT into Cdb_global_sessions (Sessionkey,sessionexptime,sessionarray) VALUES (". $SessionKey. ",". $ Sessionexptime. ",". $SessionArray. ")";
$Result = mysql_query ($Query, $DB _select_db);

if (! $Result) {
$Query = "UPDATE cdb_global_sessions SET sessionexptime =". $SessionExpTime. ", Sessionarray =". $SessionArray. " WHERE SessionKey = ". $SessionKey." and Sessionexptime >. " Time ();
$Result = mysql_query ($Query, $DB _select_db);
}
return $Result;
}

function Sess_destroy ($SessionKey) {
Global $DB _select_db;

$Query = "DELETE from cdb_global_sessions WHERE sessionkey =". $SessionKey. "";
$Result = mysql_query ($Query, $DB _select_db);

return $Result;
}

function sess_gc ($maxlifetime) {
Global $DB _select_db;

$Query = "DELETE from cdb_global_sessions WHERE sessionexptime <". Time ();
$Result = mysql_query ($Query, $DB _select_db);

Return Mysql_affected_rows ($DB _select_db);
}

Session_set_save_handler (
"Sess_open",
"Sess_close",
"Sess_read",
"Sess_write",
"Sess_destroy",
"SESS_GC");

Session_Start ();
?>

http://www.bkjia.com/PHPjc/508230.html www.bkjia.com true http://www.bkjia.com/PHPjc/508230.html techarticle ? $DB _server = "Server";/* Database SERVER hostname */$DB _name = "dbname";/* Database NAME */$DB _user = "root";/* Database user */$DB _pass = "*************"; /* Database Pass ...

  • Related Article

    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.