Method for saving session using database

zhaozj2021-02-08  208

Author:

Liuphy Posted Date:

Dance on December 20, 2000 Reading:

98

PHP's session defaults to save on the server in a file manner, and use a cookie to save variables on the client, which will have a problem. When a user closes the browser cookie, the session related operations in the program Will not be executed. Therefore, if the session data can be saved in a database, it will not be limited by the client, and there is a leap in terms of performance and scalability. The use of the key function in the program is session_set_save_handler, and it is necessary to change the session.save_handler = files in php.ini to User. The environment we discussed here is Linux (Free) Apache MySQL PHP.

Data Sheet 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] can't connect to $ sess_dbhost as $ sess_dbuser"; echo "

  • mysql error:". Mysql_error (); Die; } {Echo "
  • unable to select database $ sess_dbname";} function sess_close ()} function sess_close ()} function sess_close ()} function sess_close;} function sess_close Sess_read ($ key) {Global $ sess_dbh, $ sess_life; $ qry = "select value from session_tbl where sesskey = '$ key' and expect>". Time (); $ q= mysql_query ($ qry, $ sess_dbh); if (List ($ VALUE) = mysql_fetch_row ($ q)) {Return False;} Function Sess_Write ($ KEY, $ VAL) {Global $ sess_dbh, $ sess_life; $ eviry = Time () $ sess_life; // Expired Time $ Value = AddSlashes ($ VAL); $ Qry = "Insert INTO Session_TBL VALUES ('$ Key', $ EXPIRY, '$ VALUE')"; $ q =

    Mysql_Query ($ QRY, $ sess_dbh); if (! $ qid) {$ qry = "Update session_tbl set expression = $ eviry, value = '$ value' where sesskey = '$ key' and expression>". 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, in the program Using Require ("session_inc.php") instead of session_start (), other session functions are also called previous methods. [The copyright belongs to the copyright of this article and the Ozo network share, if you need to reprint, please indicate the author and the source]

    Explanation: It is conducive to user online communication, and statistics online number

  • 转载请注明原文地址:https://www.9cbs.com/read-2961.html

    New Post(0)