Ways to save sessions using a database

Source: Internet
Author: User
Tags mysql php session sessions
PHP session By default is stored in the server side of the file, and in the client using cookies to save variables, this will be a problem, when a user for some security reasons to shut down the browser's cookies, the program's session-related operations will not be able to execute. Therefore, if you can save session data in a database, you will not be limited by the client settings and have a leap in performance and scalability. The key function 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 the Linux (FREESD) +apache+mysql+php.

Datasheet 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; Expiration 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 steps, use require ("session_inc.php") in the program instead of Session_Start (), and the other session functions are called as before.



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.