Maison > Article > développement back-end > 关于Session入库的有关问题
关于Session入库的问题
PHP学了一段时间,机子尝试着基于MVC思想写框架,但是session入库碰到一些问题,求大神帮忙看看
这是SessionDBTool.class.php文件代码:
<?php<br />class SessionDBTool {<br /> //MySQLDB类的对象<br /> private $db;<br /><br /> public function __construct(){<br /> //设置处理器方法<br /> ini_set('session.save_handler', 'user');<br /> session_set_save_handler(<br /> array($this,'sess_open'),<br /> array($this,'sess_close'),<br /> array($this,'sess_read'),<br /> array($this,'sess_write'),<br /> array($this,'sess_destroy'),<br /> array($this,'sess_gc')<br /> );<br /> //开启session<br /> session_start();<br /> }<br /><br /> public function sess_open(){<br /> $this->db = MySQLDB::getInstance($GLOBALS['config']['database']);<br /> }<br /><br /> public function sess_close(){<br /> return true;<br /> }<br /><br /> public function sess_read($sess_id){<br /> $sql = "select sess_data from it_session where sess_id = '$sess_id'";<br /> return (string)$this->db->fetchColumn($sql);<br /> }<br /><br /> public function sess_write($sess_id,$sess_data){<br /> $expire = time();<br /> $sql = "insert into it_session values('$sess_id','$sess_data',$expire) on duplicate key update sess_data = '$sess_data',expire = $expire";<br /> return $this->db->query($sql);<br /> }<br /><br /> public function sess_destroy($sess_id){<br /> $sql = "delete from it_session where sess_id = '$sess_id'";<br /> return $this->db->query($sql);<br /> }<br /><br /> public function sess_gc($ttl){<br /> $last_time = time() - $ttl;<br /> $sql = "delete from it_session where expire < $last_time";<br /> return $this->db->query($sql);<br /> }<br />}<br /><br />?>
<?php<br />/**<br /> * Created by PhpStorm.<br /> * User: DoubleJack<br /> * Date: 2015/5/13<br /> * Time: 23:18<br /> */<br /> class BackPlatformController extends Controller {<br /> /**<br /> * 构造函数,使其自动checklogin()<br /> */<br /> public function __construct(){<br /> //开启session<br /> $this->initSession();<br /> //验证是否登陆<br /> $this->checkLogin();<br /><br /> }<br /> /**<br /> * 初始化session入库<br /> */<br /> protected function initSession(){<br /> new SessionDBTool;<br /> }<br /><br /> /**<br /> * 验证当前用户是否登陆<br /> */<br /> protected function checkLogin(){<br /> //判断是否登录<br /> if(CONTROLLER=="admin" && (ACTION=="login" || ACTION=="signin")) {<br /> //不需要<br /> }else{<br /> //需要<br /> if(isset($_SESSION['is_login']) && $_SESSION['is_login'] == 'YES'){<br /> //载入后台的模板<br /> require CURR_VIEW_DIR . 'index.html';<br /> //继续执行<br /> }else{<br /><br /> $this->jump('index.php?p=back&c=Admin&a=login','请先登录',2);<br /> }<br /> }<br /> }<br /><br /><br /><br /> }