<?php
Class Session_handler {
protected $maxlifetime = null;
protected $dbHandle = null;
public $config = null;
public static function init ($args) {
return new self ($args);
}
Public function __construct ($args) {
$this->config = $args;
$this->maxlifetime = Get_cfg_var ("Session.gc_maxlifetime");
Session_set_save_handler (
Array ($this, "open"),
Array ($this, "close"),
Array ($this, "read"),
Array ($this, "write"),
Array ($this, "destroy"),
Array ($this, "GC"));
}
Public Function open () {
$this->link = Mysqli_connect (
$this->config[' host '],
$this->config[' user '],
$this->config[' password '],
$this->config[' database ']);
Mysqli_set_charset ($this->link, "UTF8");
$sql = ' CREATE TABLE IF not EXISTS '%s ' (
' session_id ' varchar (255) Not NULL,
' Session_data ' text,
' Session_expires ' char (%) not NULL,
PRIMARY KEY (' session_id ')
) Engine=innodb DEFAULT Charset=utf8 collate=utf8_unicode_ci; ';
$sql = sprintf ($sql, $this->config[' table '));
Mysqli_query ($this->link, $sql);
return true;
}
Public function Close () {
return true;
}
Public function read ($session _id) {
if (Empty ($session _id)) return null;
$sql = ' SELECT ' session_data ' as ' data ' from '%s ' WHERE ' session_id ' = '%s ' and ' session_expires ' > '%u ' ';
$sql = sprintf ($sql,
Mysqli_real_escape_string ($this->link, $this->config[' table '),
Mysqli_real_escape_string ($this->link, $session _id),
Time ());
$result = Mysqli_query ($this->link, $sql);
$row = Mysqli_fetch_assoc ($result);
return $row [' data '];
}
Public Function Write ($session _id, $session _data) {
if (Empty ($session _id)) return null;
$newExpires = time () + $this->maxlifetime;
$sql = ' REPLACE into '%s ' SET ' session_id ' = '%s ', ' session_data ' = '%s ', ' session_expires ' = '%u ';
$sql = sprintf ($sql,
Mysqli_real_escape_string ($this->link, $this->config[' table '),
Mysqli_real_escape_string ($this->link, $session _id),
Mysqli_real_escape_string ($this->link, $session _data),
$newExpires);
$result = Mysqli_query ($this->link, $sql);
Return Mysqli_affected_rows ($this->link);
}
Public function Destroy ($session _id) {
$sql = ' DELETE from '%s ' WHERE ' session_id ' = '%s ' ';
$sql = sprintf ($sql,
Mysqli_real_escape_string ($this->link, $this->config[' table '),
Mysqli_real_escape_string ($this->link, $session _id));
$result = Mysqli_query ($this->link, $sql);
Return Mysqli_affected_rows ($this->link);
}
Public Function GC () {
$sql = ' DELETE from '%s ' WHERE ' session_expires ' < '%u ';
$sql = sprintf ($sql,
Mysqli_real_escape_string ($this->link, $this->config[' table '),
Time ());
$result = Mysqli_query ($this->link, $sql);
Return Mysqli_affected_rows ($this->link);
}
}
Class Session {
public static $collection = null;
public static function open ($clean = False, $token = False) {
if ($clean) Ob_end_clean ();
if ($token) session_id ($token);
Session_Start ();
Self:: $collection = $_session;
}
public static function ID () {
$num _args = Func_num_args ();
if ($num _args) {
$args = Func_get_arg (0);
return session_id ($args);
}else{
return session_id ();
}
}
public static function Get ($name) {
return Isset ($_session[$name])? $_session[$name]: null;
}
public static function set ($name, $value) {
$_session[$name] = $value;
return true;
}
public static function Delete ($name) {
if (!isset ($_session[$name]) return null;
Unset ($_session[$name]);
return true;
}
public static function Destroy () {
Session_destroy ();
}
}
$config = Array (
"Host" => "127.0.0.1",
"User" => "root",
"Password" => "123456",
"Database" => "test",
"CharSet" => "UTF8",
"Table" => "user_session");
Session_handler::init ($config);
Session::open ();
Session::set ("Profile", Array ("id" => 1, "User" => "Haowei", "Vip-level" => 6));