How to use the database method to save the session

Source: Internet
Author: User
Tags php session
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]

$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 "Can ' t connect to $SESS _dbhost as $SESS _ DBUSER "; echo" MySQL Error: ". Mysql_error ();d ie;}
if (! mysql_select_db ($SESS _dbname, $SESS _dbh)) {echo "Unable to select database $SESS _dbname";d ie;} 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 ex Piry > ". 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 $value = Addslashes ($va l); $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 in the same way as before.

This article describes how to save the session through the database, more relevant content please follow the PHP Chinese web.

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.