Code implementation
First create the data table, the MySQL SQL statement is as follows:
The code is as follows |
Copy Code |
CREATE TABLE ' Sess ' ( ' Sesskey ' varchar not NULL default ', ' Expiry ' bigint not NULL default ' 0′, ' Data ' longtext not NULL, PRIMARY KEY (' Sesskey '), KEY ' expiry ' (' expiry ') ) Type=myisam |
Sesskey is session id,expiry for session expiration, and data is used to save session data.
By default, session data is saved as a file, and you want to save it using the database, you must redefine the processing functions for each operation of the session. PHP provides the Session_set_save_handle () function, you can use this function to customize the session process, of course, first of all to change the Session.save_handler to user, you can set in PHP:
The code is as follows |
Copy Code |
<?php Session_module_name (' user '); ?>
|
Next, let's focus on the Session_set_save_handle () function, which has six parameters:
Session_set_save_handler (string Open, string close, string read, string write, string destroy, String gc)
Each parameter is the function name of each operation, which is open, close, read, write, destroy, Garbage collected. There are detailed examples in the PHP manual where we use OO to implement these operations, and the detailed code is as follows:
The code is as follows |
Copy Code |
<?php Define (' My_sess_time ', 3600); Session Survival Time Long Class definition Class My_sess { function init () { $domain = '. infor96.com '; Do not use the Get/post variable method Ini_set (' Session.use_trans_sid ', 0); Set maximum lifetime for garbage collection Ini_set (' Session.gc_maxlifetime ', my_sess_time); How to save session IDs with cookies Ini_set (' session.use_cookies ', 1); Ini_set (' Session.cookie_path ', '/'); Multi-host sharing cookies that hold session IDs Ini_set (' Session.cookie_domain ', $domain); Set the Session.save_handler to user instead of the default files Session_module_name (' user '); Defines the name of the method corresponding to the session's actions: Session_set_save_handler ( Array (' my_sess ', ' open '),//corresponds to the static method My_sess::open (), below. Array (' my_sess ', ' close '), Array (' my_sess ', ' read '), Array (' my_sess ', ' write '), Array (' my_sess ', ' destroy '), Array (' my_sess ', ' GC ') ); }//end function function open ($save _path, $session _name) { return true; }//end function function Close () { Global $MY _sess_conn; if ($MY _sess_conn) {//Close database connection $MY _sess_conn->close (); } return true; }//end function function Read ($sesskey) { Global $MY _sess_conn; $sql = ' SELECT data from Sess WHERE sesskey= '. $MY _sess_conn->qstr ($sesskey). ' and expiry>= '. Time (); $rs =& $MY _sess_conn->execute ($sql); if ($rs) { if ($rs->eof) { Return "; } else {//Read the session data corresponding to the session ID $v = $rs->fields[0]; $rs->close (); return $v; }//end If }//end If Return "; }//end function function Write ($sesskey, $data) { Global $MY _sess_conn; $qkey = $MY _sess_conn->qstr ($sesskey); $expiry = time () + my_sess_time; Set Expiration Time Write session $arr = Array ( ' Sesskey ' => $qkey, ' Expiry ' => $expiry, ' Data ' => $data); $MY _sess_conn->replace (' Sess ', $arr, ' sesskey ', $autoQuote = true); return true; }//end function function Destroy ($sesskey) { Global $MY _sess_conn; $sql = ' DELETE from Sess WHERE sesskey= '. $MY _sess_conn->qstr ($sesskey); $rs =& $MY _sess_conn->execute ($sql); return true; }//end function function gc ($maxlifetime = NULL) { Global $MY _sess_conn $sql = ' DELETE from Sess WHERE expiry< '. Time (); $MY _sess_conn->execute ($sql); //Because of the frequent deletion of the table sess, it is easy to produce fragmentation, //So the table is optimized for garbage collection. $sql = ' OPTIMIZE TABLE sess '; $MY _sess_conn->execute ($sql); return true; }//end function }///:~ ///use ADODB as the database abstraction layer. require_once (' adodb/adodb.inc.php '); A //database configuration entry, which can be placed in a configuration file (such as: config.inc.php). $db _type = ' MySQL '; $db _host = ' 192.168.212.1 '; $db _user = ' sess_user '; $db _pass = ' sess_pass '; $db _name = ' sess_db '; //Create a database connection, which is a global variable. $GLOBALS [' My_sess_conn '] =& adonewconnection ($db _type); $GLOBALS [' My_sess_conn ']->connect ($db _host, $db _user, $db _pass, $db _name); //Initialize session settings, must be run before session_start ()!! My_sess::init (); ? |