薛伟 发表于 2011-9-21 18:00:31

用session判断是否登录

CI中session判断用户是否登录放在哪个地方来方便所有的控制器判断,是用钩子么?求助

yuzhigang5460 发表于 2011-9-21 19:48:39

本帖最后由 yuzhigang5460 于 2011-9-21 19:50 编辑

大概stblog里的方式比较经典。
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 ;
当然你可以使用弱化的登录判断;
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;
      }
}

dogwin 发表于 2011-9-21 22:43:44

用数据库存session

appleboy 发表于 2011-9-22 15:09:42

可以試試看這個

https://github.com/benedmunds/CodeIgniter-Ion-Auth

longjianghu 发表于 2011-9-22 17:47:00

if(empty($_SESSION['uid'])){
跳转代码
}

saixs 发表于 2011-9-26 10:23:30

顺路学习下,刚准备做个登陆的东西学习下ci

toby3582 发表于 2013-10-5 12:10:48

帖子好少哦

toby3582 发表于 2013-10-5 12:23:11

:hug::o
页: [1]
查看完整版本: 用session判断是否登录