Home > Article > Backend Development > Custom storage method of PHP SESSION_PHP tutorial
In PHP, the data in the session is stored on the disk in the form of files by default, which may be enough for small websites. Big thing: For large and medium-sized websites, or some websites with special needs, the default storage method can no longer meet the requirements! They need to define how sessions are stored themselves so that the data in these sessions can be shared among multiple servers!
PHP provides a function to solve this
bool session_set_save_handler ( string open, string close, string read, string write, string destroy, string gc )
Each parameter in the function corresponds to a function. The name of the function is customizable but the parameters are in a fixed format, even if they are not used.
1. open(string save_path, string session_name) is session_start parameter string save_path session access path string session_name passes the cookie name of session id.
function _session_open(string save_path, string name)
{
$db=mysql_connect("localhost","root","123456","tendao");
return TRUE;
}
2. close is session_close without parameters
The database should be closed here, but in general websites, it is generally not closed here.
3. read(key) reads the session key value. The key corresponds to the session id.
4. The data in write(key, date) corresponds to the set session variable. The format is as follows:
email_name|s:13:"qqtxt@163.com";member_id|s:1:"1";
5. destroy(key) log out session
The corresponding record items are deleted in this program segment.
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;The character set is changed according to the situation
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()).'
'.strval($_SESSION['time']);
?>
All rights reserved by Zero Space