用户
 找回密码
 入住 CI 中国社区
搜索
查看: 10954|回复: 7
收起左侧

[HELP] 用session判断是否登录

[复制链接]
发表于 2011-9-21 18:00:31 | 显示全部楼层 |阅读模式
CI中session判断用户是否登录放在哪个地方来方便所有的控制器判断,是用钩子么?求助
发表于 2011-9-21 19:48:39 | 显示全部楼层
本帖最后由 yuzhigang5460 于 2011-9-21 19:50 编辑

大概stblog里的方式比较经典。
PHP复制代码
class ST_Auth_Controller extends Controller {
 protected function __construct() {
       
                parent::Controller();
               
                /** 加载验证库 */
                $this->load->library('auth');
               
                /** 检查登陆 */                
                if(!$this->auth->hasLogin())
                {
                        redirect('admin/login?ref='.urlencode($this->uri->uri_string()));
                }
 …
}
}
复制代码

Auth里有用session判断访问权限的代码,其他需登录的控制器继承于ST_Auth_Controller ;
当然你可以使用弱化的登录判断;
PHP复制代码
class SP_Controller extends CI_Controller {
        protected $_profile = array();
        protected $_data = array('page_title'=>'');
        public function __construct()
        {
                parent::__construct();
                $this->_pre_control();
        }
        protected function _pre_control()
        {
                $this->_profile = $this->session->userdata('profile');
                if(!empty($profile))
                {
                        $this->_data['user'] = unserialize($profile);
                        $this->_data['has_login'] = true;
                }
                else
                        $this->_data['has_login'] = false;
        }
}
复制代码
发表于 2011-9-21 22:43:44 | 显示全部楼层
用数据库存session
发表于 2011-9-22 15:09:42 | 显示全部楼层
可以試試看這個

https://github.com/benedmunds/CodeIgniter-Ion-Auth
发表于 2011-9-22 17:47:00 | 显示全部楼层
if(empty($_SESSION['uid'])){
跳转代码
}
发表于 2011-9-26 10:23:30 | 显示全部楼层
顺路学习下,刚准备做个登陆的东西学习下ci
发表于 2013-10-5 12:10:48 | 显示全部楼层
帖子好少哦
发表于 2013-10-5 12:23:11 | 显示全部楼层

本版积分规则