Php uses MySQL to save session. This function is generally available in many large systems, but it needs to be separated for analysis, there are not many materials on the internet. here I have compiled an article to share it with you.
Using MySQL to save session sessions has many advantages over files:
1) for distributed systems, files can only be stored on one machine.
2) for systems with high traffic volumes, when files are used, each session is saved in a file, and the directory is too large, it is difficult to find the session file.
Php code
///// Use MySQL to save a session. First, you must create a session table:
//////// MysqlSession class is as follows:
DB_NAME = & $ sessionDB; $ this-> SESS_LIFE = get_cfg_var ("session. gc_maxlifetime "); session_module_name ('user'); session_set_save_handler (array (& $ this, 'sess _ open'), array (& $ this, 'sess _ close '), array (& $ this, 'sess _ READ'), array (& $ this, 'sess _ write'), array (& $ this, 'sess _ destroy '), array (& $ this, 'sess _ gc '); session_start ();} function sess_open ($ save_path, $ session_name) {// open the database connection if (! $ This-> DB_SELECT_DB = mysql_pconnect ($ this-> DB_SERVER, $ this-> DB_USER, $ this-> DB_PASS) {echo "SORRY! Mysql error: Can't connect to $ this-> DB_SERVER as $ DB_USER "; echo" MySQL Error: ", mysql_error (); die;} if (! Mysql_select_db ($ this-> DB_NAME, $ this-> DB_SELECT_DB) {echo "SORRY! Mysql error: Unable to select database $ this-> DB_NAME "; die;} return true;} function sess_close () {return true;} function sess_read ($ SessionKey) {$ Query = "SELECT SessionArray FROM sessions WHERE SessionKey = '". $ SessionKey. "'AND SessionExpTime> ". time (); // do not read after expiration. $ Result = mysql_query ($ Query, $ this-> DB_SELECT_DB); if (list ($ SessionArray) = mysql_fetch_row ($ Result) {return $ SessionArray;} return false ;} function sess_write ($ SessionKey, $ VArray) {$ SessionExpTime = time () + $ this-> SESS_LIFE; // update the Session expiration time, session expiration time = last update time + maximum Session usage time $ SessionArray = addslashes ($ VArray); $ Query = "insert into SessionKey, SessionExpTime, SessionArray) VALUES ('". $ SessionKey. "','". $ SessionExpTime. "','". $ SessionArray. "')"; $ Result = mysql_query ($ Query, $ this-> DB_SELECT_DB); if (! $ Result) {$ Query = "UPDATE sessions SET SessionExpTime = '". $ SessionExpTime. "', SessionArray = '". $ SessionArray. "'Where SessionKey = '". $ SessionKey. "'AND SessionExpTime> ". time (); $ Result = mysql_query ($ Query, $ this-> DB_SELECT_DB);} return $ Result;} function sess_destroy ($ SessionKey) {$ Query = "delete from sessions WHERE SessionKey = '". $ SessionKey. "'"; $ Result = mysql_query ($ Query, $ this-> DB_SELECT_DB); return $ Result;} function sess_gc ($ maxlifetime) {// system call of this garbage collector. It is cleared once every 1440 seconds by default. You can set the parameters in PHP. ini. $ Query = "delete from sessions WHERE SessionExpTime <". time (); $ Result = mysql_query ($ Query, $ this-> DB_SELECT_DB); return mysql_affected_rows ($ this-> DB_SELECT_DB); }}?> // Usage: replace session_start with $ session = new MysqlSession (). // Note: you must open the database before the program is included. the database cannot be closed before the main program exits, otherwise, an error occurs.