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] 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 "
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