Database save session How to save session using database

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 ();
    Die
    }
    if (! mysql_select_db ($SESS _dbname, $SESS _dbh)) {
    echo "
  • 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 in the same way as before.
    "The copyright of this article is owned by the author and house Orso near net, if need to reprint, please specify the author and source"

    The above describes the database save session using the database to save the session, including the database to save the session content, I hope 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.