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

session数据库类

PHP中文网
PHP中文网Original
2016-05-25 17:13:511058browse

[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();

                   

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn