PHP 使用MySQL管理Session的回调函数详解
程序员文章站
2023-10-17 17:45:11
复制代码 代码如下:
<?php
class mysession extends dbsql {
/**
* __construct()
*/
public function __construct() {
parent::__construct ();
}
/**
* open()
*
* @param <string> $ssavepath
* @param <string>$ssessionnames
*
* @return <boolean> true
*/
public function open($ssavepath, $ssessionname) {
return true;
}
/**
* close()
*/
public function close() {
return true;
}
/**
* read()
*
* @param <string> $ssessionid
* @return <string> session data or null on error...
*/
public function read($ssessionid) {
$ssessionid = addslashes(trim($ssessionid));
$ssql = "select * from tb_session where f_sessionid = '{$ssessionid}'";
//echo $ssql;
if(false != ($result = $this->select($ssql))) {
//var_dump($result[0]['f_sessiondata']);
return $result[0]['f_sessiondata'];
} else {
return '';
}
}
/**
* write()
*
* @param <string> $sessionid
* @param <string> $ssessiondata
*
* @return <boolean> true or false
*/
public function write($ssessionid, $ssessiondata) {
$arrsession = array();
$arrsession['f_sessionid'] = addslashes(trim($ssessionid));
$arrsession['f_sessiondata'] = addslashes($ssessiondata);
$arrsession['f_sessioncreated'] = date("y-m-d h:i:s");
$arrsession['f_sessionupdated'] = date("y-m-d h:i:s", time() + 1440);
// 新session及更新的session
if (false != ($result = $this->insertdata('tb_session', $arrsession))) {
return true;
} else {
$arrupdatesession = array();
$arrupdatesession['f_sessiondata'] = addslashes($ssessiondata);
$arrupdatesession['f_sessionupdated'] = date("y-m-d h:i:s", time() + 1440);
if (false != ($result = $this->updatedata('tb_session', "'".addslashes($ssessionid)."'", 'f_sessionid', $arrupdatesession))) {
return true;
} else {
return false;
}
}
}
/**
* destroy()
*
* @param <string> $ssessionid
* @return <boolean> true
*/
public function destroy($ssessionid) {
$ssessionid = addslashes(trim($ssessionid));
$this->deldata($ssessionid, 'f_sessionid', 'tb_session');
return true;
}
/**
* gc
*
* @param <int> $nmaxlifetime seconds
*
* @return <boolean> true
*/
public function gc($nmaxlifetime) {
$dtexpiredtime = date("y-m-d h:i:s", time() - $nmaxlifetime);
$ssql = "delete from tb_session where f_sessionupdated < {$dtexpiredtime}";
//echo $ssql;
$this->delete($ssql);
return true;
}
/**
* __desctruct()
*
*/
function __destruct() {
// ensure session data is written out before classes are destroyed
// (see http://bugs.php.net/bug.php?id=33772 for details)
@session_write_close();
} // __destruct
}
?>
复制代码 代码如下:
<?php
class mysession extends dbsql {
/**
* __construct()
*/
public function __construct() {
parent::__construct ();
}
/**
* open()
*
* @param <string> $ssavepath
* @param <string>$ssessionnames
*
* @return <boolean> true
*/
public function open($ssavepath, $ssessionname) {
return true;
}
/**
* close()
*/
public function close() {
return true;
}
/**
* read()
*
* @param <string> $ssessionid
* @return <string> session data or null on error...
*/
public function read($ssessionid) {
$ssessionid = addslashes(trim($ssessionid));
$ssql = "select * from tb_session where f_sessionid = '{$ssessionid}'";
//echo $ssql;
if(false != ($result = $this->select($ssql))) {
//var_dump($result[0]['f_sessiondata']);
return $result[0]['f_sessiondata'];
} else {
return '';
}
}
/**
* write()
*
* @param <string> $sessionid
* @param <string> $ssessiondata
*
* @return <boolean> true or false
*/
public function write($ssessionid, $ssessiondata) {
$arrsession = array();
$arrsession['f_sessionid'] = addslashes(trim($ssessionid));
$arrsession['f_sessiondata'] = addslashes($ssessiondata);
$arrsession['f_sessioncreated'] = date("y-m-d h:i:s");
$arrsession['f_sessionupdated'] = date("y-m-d h:i:s", time() + 1440);
// 新session及更新的session
if (false != ($result = $this->insertdata('tb_session', $arrsession))) {
return true;
} else {
$arrupdatesession = array();
$arrupdatesession['f_sessiondata'] = addslashes($ssessiondata);
$arrupdatesession['f_sessionupdated'] = date("y-m-d h:i:s", time() + 1440);
if (false != ($result = $this->updatedata('tb_session', "'".addslashes($ssessionid)."'", 'f_sessionid', $arrupdatesession))) {
return true;
} else {
return false;
}
}
}
/**
* destroy()
*
* @param <string> $ssessionid
* @return <boolean> true
*/
public function destroy($ssessionid) {
$ssessionid = addslashes(trim($ssessionid));
$this->deldata($ssessionid, 'f_sessionid', 'tb_session');
return true;
}
/**
* gc
*
* @param <int> $nmaxlifetime seconds
*
* @return <boolean> true
*/
public function gc($nmaxlifetime) {
$dtexpiredtime = date("y-m-d h:i:s", time() - $nmaxlifetime);
$ssql = "delete from tb_session where f_sessionupdated < {$dtexpiredtime}";
//echo $ssql;
$this->delete($ssql);
return true;
}
/**
* __desctruct()
*
*/
function __destruct() {
// ensure session data is written out before classes are destroyed
// (see http://bugs.php.net/bug.php?id=33772 for details)
@session_write_close();
} // __destruct
}
?>
下一篇: Flash教程:if条件语句的用法
推荐阅读
-
PHP 使用MySQL管理Session的回调函数详解
-
Python的Twisted框架中使用Deferred对象来管理回调函数
-
PHP 使用MySQL管理Session的回调函数详解_PHP教程
-
在php中使用swoole扩展时,server端的回调函数中如何使用thinkphp框架的方法?
-
PHP 使用MySQL管理Session的回调函数详解
-
PHP 使用MySQL管理Session的回调函数详解_php技巧
-
PHP的回调函数使用详解
-
Python的Twisted框架中使用Deferred对象来管理回调函数
-
PHP 使用MySQL管理Session的回调函数详解_php技巧
-
关于回调 ob_start() 函数的加强使用_PHP教程