Rumah > pangkalan data > tutorial mysql > session存储到mysql数据库_MySQL

session存储到mysql数据库_MySQL

WBOY
Lepaskan: 2016-05-31 08:46:25
asal
1126 orang telah melayarinya

根据qq互联中的session类和php官方上的例子,整理

<?phpini_set (&#39;error_reporting&#39;,E_ALL);ini_set(&#39;display_errors&#39;,TRUE);class session{const db_host = &#39;127.0.0.1&#39;;const db_user = &#39;root&#39;;const db_password =&#39;&#39;;const db_name = &#39;test&#39;;const db_table = &#39;tbl_session&#39;;private $lifetime;private $dbHandle;function open($path,$id){  $con = @mysql_connect(self::db_host,self::db_user,self::db_password);  $sel = @mysql_select_db(self::db_name,$con);  $this->lifetime = get_cfg_var("session.gc_maxlifetime");  $this->dbHandle = $con;  if(!$con || !$sel)      return false;      return true; }function close(){    $this->gc($this->lifetime);	return @mysql_close($this->dbHandle);}function read($id){   $rs = @mysql_query("select session_data data from ".self::db_table." where session_expires dbHandle);    $row = @mysql_fetch_assoc($rs);	return $row?$row['data']:'';}function write($id,$data){	$newEXP = time()+ ini_get("session.gc_maxlifetime");   $sel = mysql_query("select * from ".self::db_table." where session_id= '".$id."'",$this->dbHandle);   if(mysql_affected_rows($this->dbHandle)){     $rs = @mysql_query("update ".self::db_table." set session_data = '".$data."', session_expires = '".$newEXP."' WHERE session_id = '".$id."'",$this->dbHandle);	  if(@mysql_affected_rows($this->dbHandle))		  return true;   }else{     $rs = @mysql_query("insert into ".self::db_table." (session_id,session_data,session_expires)values('".$id."','".$data."','".$newEXP."')",$this->dbHandle);	 if(@mysql_affected_rows($rs))		 return true;   }   return false;}function destroy($id){	@mysql_query("delete from ".self::db_table." where session_id = '".$id."'",$this->dbHandle);}function gc($lifetime){    @mysql_query("delete from ".self::db_table." where sessoin_expires > '".time()."'",$this->dbHandle);}}$session = new session();session_set_save_handler(array(&$session,'open'),array(&$session,'close'),array(&$session,'read'),array(&$session,'write'),array(&$session,'destroy'),array(&$session,'gc'));session_start();
Salin selepas log masuk



sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan