How to save a session using a database _ PHP Tutorial

Source: Internet
Author: User
Use the database to save the session. By default, php sessions are stored as files on the server side, and variables are saved using cookies on the client side. This causes a problem, when a user closes a php session for some security reason, the session is saved as a file on the server by default, and the variable is saved as a cookie on the client, this will cause a problem, when a user closes the browser cookie for some security reason, session-related operations in the program will not be executed. Therefore, if session data can be stored in a database, it will not be restricted by the client settings, and there will be a leap in performance and scalability. The key function used in the program is session_set_save_handler. at the same time, change session. save_handler = files in php. ini to user. The environment we will discuss here is linux (freesd) + apache + mysql + php.

Data table structure: [sessions]
Create table sessions (
Sesskey char (32) not null,
Expiry int (11) 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; // 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 preceding steps, use require ("session_inc.php") in the program to replace session_start (). Other session functions are called in the same way as before.


    [This article is copyrighted by the author and osuo. if you need to reprint it, please indicate the author and its source]

    ...

  • 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.