Home > Backend Development > PHP Tutorial > 关于Session入库的有关问题

关于Session入库的有关问题

WBOY
Release: 2016-06-13 12:20:04
Original
993 people have browsed it

关于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 />?>
Copy after login


这个是后台控制器类文件代码:
<?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 />    }
Copy after login


但是打开login页面的时候,没有session,登录的时候提示“请先登录”,应该是SessionDBTool没有被初始化导致了,看了很久,不知道从哪里改,好人一生平安帮忙解决
------解决思路----------------------
/**
         * 初始化session入库
         */
        protected function initSession(){
           new SessionDBTool;
        }


这段代码没有任何意义啊
------解决思路----------------------
在函数(方法)中创建的变量(对象)在函数(方法)外是访问不到的
你需要把它存放到可持续访问的载体上,比如
  $this->pile =  new SessionDBTool;
不然 initSession 执行结束后,对象也没有了

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template