@header("content-Type: text/html; charset=UTF-8");
require('includes/configure.php');
//include database tables
require('includes/database_table.php');
// set php_self in the local scope
if (!isset($PHP_SELF)) $PHP_SELF = basename($_SERVER['SCRIPT_FILENAME']);
// include the database class
require(DIR_WS_CLASSES . 'db_mysql_class.php');
$db=new DbCom();
// make a connection to the database... now
$db->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE);
$db->query("set names utf8");
$session = new session($db);
session_start();
$_SESSION['webid']=117;
$_SESSION['username']='duanjianbo';
class session {
var $db;
function session(&$db) {
$this->db = &$db;
session_module_name('user');
session_set_save_handler(
array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc')
);
session_start();
}
function unserializes($data_value) {
$vars = preg_split(
'/([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\|/',
$data_value, -1, PREG_SPLIT_NO_EMPTY |
PREG_SPLIT_DELIM_CAPTURE
);
for ($i = 0; isset($vars[$i]); $i++) {
$result[$vars[$i++]] = unserialize($vars[$i]);
}
return $result;
}
function open($path, $name) {
return true;
}
function close() {
return true;
}
function read($SessionKey){
$sql = "SELECT uid FROM sessions WHERE session_id = '".$SessionKey."' limit 1";
$query =$this->db->query($sql);
if($row=$this->db->fetch_array($query)){
return $row['uid'];
}else{
$this->db->query("insert into `sessions` set `session_id` = '$SessionKey'");
return "";
}
}
function write($SessionKey, $VArray) {
require_once(DIR_WS_CLASSES . 'db_mysql_class.php');
$db1=new DbCom();
// make a connection to the database... now
$db1->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE);
$db1->query("set names utf8");//这里为什么之前的$this->db的对象改变了
$SessionArray = addslashes($VArray);
$data=$this->unserializes($VArray);
$this->db=$db1;
$sql = "update `sessions` set ";
if(isset($data['webid'])){
$sql .= "uid = '".$data['webid']."', " ;
}
$sql.="`last_visit` = null "
. "where `session_id` = '$SessionKey'";
$this->db->query($sql);
return true;
}
function destroy($SessionKey) {
$this->db->query("delete from `sessions` where `session_id` = '$SessionKey'");
return true;
}
function gc($lifetime) {
$this->db->query("delete from `sessions` where unix_timestamp(now()) - unix_timestamp(`last_visit`) > $lifetime");
return true;
}
}