How to save a session using a database
PHP session By default is saved as a file on the server side, and in the client using cookies to save variables, this will be a problem, when a user for some security reasons closed browser cookies, session related operations in the program will not be able to execute. Therefore, if you can save session data in a database, it will not be limited by the client settings, and there is a leap in performance and scalability. The key function used in the program is Session_set_save_handler, while the Session.save_handler = files in php.ini are changed to user. The environment we are discussing here is Linux (FREESD) +apache+mysql+php.
data table structure: [Sessions]
CREATE TABLE Sessions (
Sesskey char (+) NOT NULL,
expiry int (one) unsigned NOT NULL,
Value text NOT NULL,
PRIMARY KEY (Sesskey)
);
Program code: [session_inc.php]
<?php
$SESS _dbhost = "Yourhost"; /* Database server hostname */
$SESS _dbname = "Yourdb"; /* Database name */
$SESS _dbuser = "Youruser"; /* Database user */
$SESS _dbpass = "YourPassword"; /* Database Password */
$SESS _DBH = "";
$SESS _life = Get_cfg_var ("Session.gc_maxlifetime");
function Sess_open ($save _path, $session _name) {
Global $SESS _dbhost, $SESS _dbname, $SESS _dbuser, $SESS _dbpass, $SESS _dbh;
if (! $SESS _DBH = mysql_pconnect ($SESS _dbhost, $SESS _dbuser, $SESS _dbpass)) {
echo "<li>can ' t connect to $SESS _dbhost as $SESS _dbuser";
echo "<li>mysql Error:". Mysql_error ();
Die
}
if (! mysql_select_db ($SESS _dbname, $SESS _dbh)) {
echo "<li>unable to select Database $SESS _dbname";
Die
}
return true;
}
function Sess_close () {
return true;
}
function Sess_read ($key) {
Global $SESS _DBH, $SESS _life;
$qry = "Select value from session_tbl WHERE sesskey = ' $key ' and expiry >". Time ();
$qid = mysql_query ($qry, $SESS _dbh);
if (list ($value) = Mysql_fetch_row ($qid)) {
return $value;
}
return false;
}
function Sess_write ($key, $val) {
Global $SESS _DBH, $SESS _life;
$expiry = time () + $SESS _life; Expiry time
$value = Addslashes ($val);
$qry = "INSERT into session_tbl VALUES (' $key ', $expiry, ' $value ')";
$qid = mysql_query ($qry, $SESS _dbh);
if (! $qid) {
$qry = "UPDATE session_tbl SET expiry = $expiry, value = ' $value ' WHERE sesskey = ' $key ' and expiry >". Time ();
$qid = mysql_query ($qry, $SESS _dbh);
}
return $qid;
}
function Sess_destroy ($key) {
Global $SESS _DBH;
$qry = "DELETE from session_tbl WHERE sesskey = ' $key '";
$qid = mysql_query ($qry, $SESS _dbh);
return $qid;
}
function sess_gc ($maxlifetime) {
Global $SESS _DBH;
$qry = "DELETE from session_tbl WHERE expiry <". Time ();
$qid = mysql_query ($qry, $SESS _dbh);
Return Mysql_affected_rows ($SESS _dbh);
}
Session_set_save_handler (
"Sess_open",
"Sess_close",
"Sess_read",
"Sess_write",
"Sess_destroy",
"SESS_GC");
Session_Start ();
?>
After completing the above steps, use require ("session_inc.php") in the program instead of Session_Start (), and the other session functions are called as before.
Dual L5520 4*1t RAID 48G 10M 128ip Special 1800 yuan/month. Contact Lao Zhang-2881064151
How to save a session using a database introduction