时间:2021-07-01 10:21:17 帮助过:22人阅读
class SessionDBTool {
//MySQLDB类的对象
private $db;
public function __construct(){
//设置处理器方法
ini_set('session.save_handler', 'user');
session_set_save_handler(
array($this,'sess_open'),
array($this,'sess_close'),
array($this,'sess_read'),
array($this,'sess_write'),
array($this,'sess_destroy'),
array($this,'sess_gc')
);
//开启session
session_start();
}
public function sess_open(){
$this->db = MySQLDB::getInstance($GLOBALS['config']['database']);
}
public function sess_close(){
return true;
}
public function sess_read($sess_id){
$sql = "select sess_data from it_session where sess_id = '$sess_id'";
return (string)$this->db->fetchColumn($sql);
}
public function sess_write($sess_id,$sess_data){
$expire = time();
$sql = "insert into it_session values('$sess_id','$sess_data',$expire) on duplicate key update sess_data = '$sess_data',expire = $expire";
return $this->db->query($sql);
}
public function sess_destroy($sess_id){
$sql = "delete from it_session where sess_id = '$sess_id'";
return $this->db->query($sql);
}
public function sess_gc($ttl){
$last_time = time() - $ttl;
$sql = "delete from it_session where expire < $last_time";
return $this->db->query($sql);
}
}
?>
/**
* Created by PhpStorm.
* User: DoubleJack
* Date: 2015/5/13
* Time: 23:18
*/
class BackPlatformController extends Controller {
/**
* 构造函数,使其自动checklogin()
*/
public function __construct(){
//开启session
$this->initSession();
//验证是否登陆
$this->checkLogin();
}
/**
* 初始化session入库
*/
protected function initSession(){
new SessionDBTool;
}
/**
* 验证当前用户是否登陆
*/
protected function checkLogin(){
//判断是否登录
if(CONTROLLER=="admin" && (ACTION=="login" || ACTION=="signin")) {
//不需要
}else{
//需要
if(isset($_SESSION['is_login']) && $_SESSION['is_login'] == 'YES'){
//载入后台的模板
require CURR_VIEW_DIR . 'index.html';
//继续执行
}else{
$this->jump('index.php?p=back&c=Admin&a=login','请先登录',2);
}
}
}
}