首頁 > php教程 > PHP源码 > 主體

session 类 数据库交互

PHP中文网
發布: 2016-05-25 17:11:16
原創
1044 人瀏覽過

跳至

query("update ".self::$table." set value='$session_data', expiry='$session_expire' where session_id='$session_id'");
			self::close();
		}
		self::$is_del = TRUE;
	}
	/**
	 * 销毁
	 * 
	 * */
	public static function destroy() 
	{
		$session_id			= self::get_session_id();
		$_COOKIE['WBSID']	= '';
		
		self::open();
		self::$dbo->query("delete from ".self::$table." where session_id='$session_id'");
		self::close();
	}
	
	
	/**
	 * 读取  私有
	 * */
	private static function read()
	{
		$session_id = self::$session_id;
		if (!$session_id) {
			$session_id = self::get_session_id();
		}
		
		if (!$session_id) return array();
		
		$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? md5($_SERVER['HTTP_USER_AGENT']) : '';
		$client_ip	= Fun::getIp();
		$session_expire = TIME - self::get_gc_maxtime();
		
		$rs = self::$dbo->fetchRow("select session_id, value, agent, ip from ".self::$table."
			where session_id='$session_id' and expiry>'$session_expire'");
		
		if (!$rs || $rs['agent'] != $user_agent || $rs['ip'] != $client_ip) 
		{
			return FALSE;
		}
		
		self::$session_id = $rs['session_id'];
		return unserialize($rs['value']);
	}
	/**
	 * session 写入   私有
	 * */
	private static function write(array $session_data) 
	{
		$session_id = self::$session_id;
		
		if (!$session_id)
		{
			$session_id = self::get_session_id();
		}
		
		$session_expire = TIME + self::get_gc_maxtime();
		
		$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? md5($_SERVER['HTTP_USER_AGENT']) : '';
		$client_ip 	= Fun::getIp();
		
		$session_data = serialize($session_data);
		
		if (self::$session_id && self::$session_id === $session_id) 
		{
			self::$dbo->query("update ".self::$table." set value='$session_data', expiry='$session_expire', agent='$user_agent', ip='$client_ip' where session_id='$session_id'");
		} 
		else 
		{
			self::$session_id = $session_id = self::create_session_id();
			self::$dbo->query("insert into ".self::$table."(session_id, value, expiry, agent, ip) 
				values('$session_id', '$session_data', '$session_expire', '$user_agent', '$client_ip')");
		}
		return true;
	}
	/**
	 * session 更新   私有
	 * */
	private static function update() 
	{
		if (!self::$is_update) 
		{
			$session_id = self::$session_id;
			$session_expire = TIME + self::get_gc_maxtime();
			self::$dbo->query("update ".self::$table." set expiry='$session_expire' where session_id='$session_id'");
		}
		self::$is_update = TRUE;
	}
	
	private static function close() 
	{
		if (!self::$is_gc && mt_rand(1, self::$gc_rate_de)%self::$gc_rate_co == 0) 
		{
			self::gc();
		}
		self::$is_gc = TRUE;
	}
	/**
	 * 过期session 清除  随机触发
	 * */
	private static function gc() 
	{
		
		$session_expire = TIME - self::get_gc_maxtime();
		 self::$dbo->query("delete from ".self::$table." where expiry<&#39;$session_expire&#39;");
	}
	
	private static function get_session_id() 
	{
		if (isset($_COOKIE[&#39;WBSID&#39;]) && strlen($_COOKIE[&#39;WBSID&#39;])==32) 
		{
			$sid = $_COOKIE[&#39;WBSID&#39;];
			setcookie(&#39;WBSID&#39;, $sid, TIME + self::get_gc_maxtime(), self::$path, self::$domain, self::$secure, self::$httponly);
			return $sid;
		}
		return null;
	}
	
	private static function create_session_id() 
	{
		$sid = self::get_session_id();
		if (!$sid) 
		{
			$sid = Fun::getIp() . TIME . microtime(TRUE) . mt_rand(mt_rand(0, 100), mt_rand(100000, 90000000));
			$sid = md5(self::$pre_key . $sid);
			setcookie(&#39;WBSID&#39;, substr($sid, 0, 32), TIME + self::get_gc_maxtime(), self::$path, self::$domain, self::$secure, self::$httponly);
		}
		return $sid;
	}
	
	public static function get_gc_maxtime()
	{
		return  self::$gc_max_time;
	}
}
登入後複製

                   

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板