首页 >php教程 >PHP源码 >session数据库类

session数据库类

PHP中文网
PHP中文网原创
2016-05-25 17:13:511041浏览

[PHP]代码 

<?php
/**
 * session类
 * @author 角度 QQ:1286522207
 *
 */
class session extends Action{
	private $session_db;
	private $session_Overdue;
	private $db_tablename;//数据库名称
	function __construct(){
		$this->session_Overdue=3600;
		$this->session_db= $this->db();
	}
	/**
	 * 打开
	 */
	public function open() {
		return true;
	}
	/**
	 * 关闭
	 */
	public function close() {
		return true;
	}
	/**
	 * 读取
	 */
	public function read($id) {
		$where[&#39;id&#39;]=$id;
		$this->session_db->M(&#39;session&#39;);
		$value=$this->session_db->find($where);
		if ($value) {
			return $value[&#39;data&#39;];
		} else {
			return "";
		}
	}
	/**
	 * 写入
	 */
	public function write($id, $datas) {
		$where[&#39;id&#39;]=$id;
		$date[&#39;data&#39;]=$datas;
		$date[&#39;time&#39;]=time()+$this->session_Overdue;
		$date[&#39;ip&#39;]=$this->session_db->get_client_ip();
		if (empty($_SESSION[&#39;user_id&#39;])){
			$date[&#39;user&#39;]=0;
		}else {
			$date[&#39;user&#39;]=$_SESSION[&#39;user_id&#39;];
		}
		$this->session_db->M(&#39;session&#39;);
		$up=$this->session_db->Update($date, $where);
		if ($up){
			return true;
		}else {
			$date[&#39;id&#39;]=$id;
			$this->session_db->Insert($date);
			return true;
		}
	}
	/**
	 * 摧毁
	 */
	public function destroy($id) {
		$where[&#39;id&#39;]=$id;
		return $this->session_db->Delete($where);
	}
	/**
	 * 回收
	 */
	public function gc($max) {
		return true;
	}
	function __destruct(){
		$this->session_db->Delete("`time`<&#39;".time()."&#39;");
	}
}
ini_set ( &#39;session.save_handler&#39;,$config[&#39;SESSION_START&#39;]);
$session = new session ();
session_set_save_handler ( array ($session, &#39;open&#39; ), array ($session, &#39;close&#39; ), array ($session, &#39;read&#39; ), array ($session, &#39;write&#39; ), array ($session, &#39;destroy&#39; ), array ($session, &#39;gc&#39; ) );
session_start();

                   

                   

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn