In php, session data is stored on the disk as files by default, which may already meet the requirements of small websites. For large and medium-sized websites or websites with special requirements, the default storage method cannot meet the requirements! They need to define their own session storage methods so that the data in these sessions can be shared among multiple servers!
Php provides a function to solve this problem.
Bool session_set_save_handler (string open, string close, string read, string write, string destroy, string gc)
A parameter in a function corresponds to a function. The function name is customized but the parameter format is fixed.
1. open (string save_path, string session_name) is the cookie name passed by session id in the session access path string session_name of the session_start parameter string save_path session.
Function _ session_open (string save_path, string name)
{
$ Db = mysql_connect ("localhost", "root", "123456", "tendao ");
Return TRUE;
}
2. No parameter is set for close to session_close.
Close the database here, but generally do not close the database here.
3. read (key) is the read session key value. Key corresponds to the session id.
4. data in write (key, date) corresponds to the set session variable, in the following format:
Email_name | s: 13: "qqtxt@163.com"; member_id | s: 1: "1 ";
5. destroy (key) cancels the session
Delete corresponding record items in this section.
6. gc (expiry_time) clears expired session records.
Table Structure 'session'
--
Create TABLE 'session '(
'Session _ key' char (32) not null,
'Session _ data' char (255) not null,
'Session _ expiry' int (11) unsigned not null,
Primary key ('session _ key ')
) ENGINE = MyISAM default charset = utf8; character set changed as needed
<? Php
Class Session {
Var $ expiry = 3600;
Var $ db;
Function _ construct (){
$ This-> Session ();
}
Function Session (){
Session_set_save_handler (array (& $ this, "_ session_open "),
Array (& $ this, "_ session_close "),
Array (& $ this, "_ session_read "),
Array (& $ this, "_ session_write "),
Array (& $ this, "_ session_destroy "),
Array (& $ this, "_ session_gc ")
);
}
/**
* Open session handler
*
* @ Param string $ save_path
* @ Param string $ session_name
* @ Return boolen
*/
Function _ session_open ($ save_path, $ session_name)
{
$ This-> db = mysql_connect ("localhost", "root", "123456") or die ("database connection failed! ");
Mysql_select_db ("tendao", $ this-> db );
Return TRUE;
}
Function _ session_close (){
Return true;
}
Function _ session_read ($ key ){
$ Expiry = time ();
$ S_query = sprintf ("select session_data from session where session_key = '% s' and session_expiry> % d", $ key, $ expiry );
$ Result = mysql_query ($ s_query, $ this-> db );
$ Row = mysql_fetch_assoc ($ result );
If ($ row ){
Return $ row ['session _ data'];
}
Else
Return FALSE;
}
Function _ session_write ($ key, $ data ){
$ Expiry_time = time () + $ this-> expiry;
$ S_query = sprintf ("select session_data from session where session_key = '% S'", $ key );
$ Result = mysql_query ($ s_query, $ this-> db );
If (mysql_num_rows ($ result) = 0 ){
$ S_query = sprintf ("insert into session values ('% s',' % s', % d)", $ key, $ data, $ expiry_time );
$ Result = mysql_query ($ s_query, $ this-> db );
}
Else {
$ S_query = sprintf ("update session set session_key = '% s', session_data =' % s', session_expiry = % d where session_key = '% S'", $ key, $ data, $ expiry_time, $ key );
$ Result = mysql_query ($ s_query, $ this-> db );
}
Return $ result;
}
Function _ session_destroy ($ key ){
$ S_query = sprintf ("delete from session where session_key = '% S'", $ key );
$ Result = mysql_query ($ s_query, $ this-> db );
Return $ result;
}
Function _ session_gc ($ expiry_time ){
$ Expiry_time = time ();
$ S_query = sprintf ("delete from session where session_expiry <% d", $ expiry_time );
$ Result = mysql_query ($ s_query, $ this-> db );
Return $ result;
}
}
$ _ Ses _ = new Session ();
Session_start ();
$ _ SESSION ['time'] = time () + 1200;
Echo strval (time (). '<br/>'. strval ($ _ SESSION ['time']);
?>
Copyright of The 0th Space