dongsilu2237
2015-06-03 12:30 阅读 27
已采纳

检查Codeigniter中另一个控制器的会话

After the user logs in, I set the variable is_logged_in=true, but in some other controller how can I check is_logged_in is true in codeigniter? Here is my login code:

public login_con extends CI_Controller
{
    public function login()
    {
        is_logged_in=true;
    }
}

I want to check this is_logged_in in another controller so how can i write code for that?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    douhuan9289 douhuan9289 2015-06-03 12:43

    Session is best solution for this. You can read codeignitor session

    // set value in session 
    $this->session->set_userdata('is_logged_in', true); 
    

    To get in other controller

    $is_logged_in  = $this->session->userdata('is_logged_in'); 
    

    Please also make sure you have loaded session library.

    $this->load->library('session');
    
    点赞 评论 复制链接分享
  • dongzhan7909 dongzhan7909 2015-06-03 12:47

    First thing autoload applications/config/autoload.php, to add session library

    $autoload['libraries'] = array('session');
    

    This will include session on every page.

    Now your controller file

    controller1

        public login_con extends CI_Controller
        {
            public function login()
            {
               //here you set session like that
               $data['is_logged_in'] = TRUE;
               $this->session->set_userdata($data);
            }
        }
    

    Here you get your is_logged_in session on other controller

    controller2

    public your_con extends CI_Controller
            {
                public function your_function()
                {
                   //here you get session like that
                  if($this->session->userdata("is_logged_in"))
                    {
                      // your code here
                     }
                }
            }
    
    点赞 评论 复制链接分享

相关推荐