分类:
2006-02-07 15:05:10
有关session的存储过程:
当增加一条记录时,先是从read函数读取session_id
然后才调用write函数,开始先判断是否有这个id,如果有就执行update,如果没有就执行insert操作,
讲了这些大家可以对session的操作会多了解一点,如果哪里讲的不好,请大家指正。
CREATE TABLE `session` (
`session_id` varchar(32) binary NOT NULL default '',
`updated_on` int(10) unsigned NOT NULL default '0',
`session_data` text,
`session_ip` varchar(15)
PRIMARY KEY (`session_id`)
) TYPE=InnoDB;
class Session
{
// --- ATTRIBUTES ---
/**
* Short description of attribute db
*
* @access private
*/
private $db;
private $life;
// --- OPERATIONS ---
public function __construct()
{
// get session-lifetime
$this->life = get_cfg_var("session.gc_maxlifetime");
//将 session.save_handler 设置为 user,而不是默认的 files
session_module_name('user');
// section -64--88-0--25--4853ec1c:108dbfe7d30:-7fec end
} // end function
public function open($savePath, $sessName)
{
require_once 'DB.php';
$this->db = DB::connect("mysql://root:test@127.0.0.1/test");
if(!$this->db)
return false;
return true;
} // end function
public function close()
{
return $this->db->disconnect();
} // end function
public function read($key)
{
$key = $this->db->quoteSmart($key);
$time = time();
$strsql = "select `session_data` from session where `session_id` = $key and `updated_on` > $time";
$result = $this->db->getOne($strsql);
if (DB::isError($result)) {
//Must return "" because php.net says so
return "";
} else {
return $result;
}
//Something slipped through the cracks, so throw an error
return "";
}// end function
public function write($key, $data)
{
$key = $this->db->quoteSmart($key);
$value = $this->db->quoteSmart($data);
$ip = $_SERVER['REMOTE_ADDR'];
$expiry = time() + $this->life; //设置过期时间
$strsql = "SELECT `updated_on` FROM session WHERE `session_id`=$key";
$result = $this->db->getOne($strsql);
if ($result === null) {
$strsql
= "INSERT INTO session (`session_id`, `updated_on`, `session_data`,
`session_ip`) VALUES ($key, '$expiry', $value, '$ip')";
$result = $this->db->query($strsql);
if (DB::isError($result)) {
//error of some sort
return false;
}
return true;
} else {
$strsql = "UPDATE session SET `updated_on`=$expiry, `session_data`=$value WHERE `session_id`=$key";
$result = $this->db->query($strsql);
//Something went wrong
if (DB::isError($result)) {
return false;
}
return true;
}
//Something slipped through, so throw an error
return false;
} //end function
public function destroy($key)
{
$key = $this->db->quoteSmart($key);
$strsql = "DELETE FROM session WHERE `session_id`=$key";
$result = $this->db->query($strsql);
if (!DB::isError($result)) {
return true;
}
return false;
} // end function
public function gc($maxlifetime)
{
$time = $this->db->quoteSmart(time());
$strsql = "DELETE FROM session WHERE `updated_on` < $time";
$result = $this->db->query($strsql);
if (DB::isError($result)) {
return 0;
}
$strsql = 'OPTIMIZE TABLE session';
$result = $this->db->query($strsql);
if (DB::isError($result)) {
return 0;
}
return $this->db->affectedRows();
} // end function
} /* end of class Session */
$session = new Session();
session_set_save_handler(array(&$session,"open"),
array(&$session,"close"),
array(&$session,"read"),
array(&$session,"write"),
array(&$session,"destroy"),
array(&$session,"gc"));
session_start();
?>
那个页面需要用的是时候,include就可以了
我连测试用例也贴出来,我们用的是smarty,smarty配置我就不说了,自己到我前面发表的文章看看吧
session_php.htm 代码如下:
session_test.php 代码如下:
require_once('include.inc.php');//此文件以前文章有,大家可以看看
require_once('session.inc.php');//这个是我上面类的文件名
session_start();
$result = '';
$action = '';
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$action = $_POST['action'];
switch ($action) {
case "increment":
$_SESSION['num'] += 1;
$result = $_SESSION['num'];
break;
case "destroy":
session_destroy();
break;
case "gc":
$maxlife = get_cfg_var("session.gc_maxlifetime");
$session->gc($maxlife);
break;
}
}
$tpl->assign("action", $action);
$tpl->assign("count", $result);
$tpl->display("session_test.htm");
?>