如何验证codeigniter中的Session?

时间:2014-01-20 08:46:38

标签: php codeigniter session

我正在尝试在Codeigniter中创建一个会话。我在自动加载中加载了sessionencrypt库。默认情况下,会打开login.php页面。如果用户已经登录,我想打开dashboard.php页面。我编写了以下代码,但即使在登录后也会打开login.php页面。

请告诉我如何检查会话是否已设置。

 public function index()
{          
   $data['products_data']= $this->products_model->all_products(); 
           //   $this->load->view('login');

   $this->form_validation->set_rules('username','Username','required');
   $this->form_validation->set_rules('password','Password','required');

    if ($this->form_validation->run()== false)
   {
       $this->load->view('login');           
   }
   else
   {
        if ($this->session->userdata('login_status')) {

            $this->load->view('header');
            $this->load->view('dashboard');
            $this->load->view('footer');

        }
        else
        {
            $username=   $this->input->post('username');
            $password=   $this->input->post('password');  

             if( $this->products_model->login_model($username,$password))
             {
               $sess_data= array(
                                'username'=> $username,
                                'login_status'=>'1'
                                );

                $session_data   =   $this->session->set_userdata($sess_data);                    

                $this->load->view('header');
                $this->load->view('dashboard');
                $this->load->view('footer');

             }

        }
   }


}

4 个答案:

答案 0 :(得分:2)

$sessdata = $this->session->set_userdata($session_data);
if (isset($sessdata))
{
//session exists
}
else{
// no session value
}

因此,您的控制器应如下所示,以避免在页面刷新时重定向到登录页面

function index() {
    if ($this->session->userdata('login_status')) {
        //write your code for redirect to dashboard
    } else {
        $data['products_data'] = $this->products_model->all_products();
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        if ($this->form_validation->run() == false) {

            $this->load->view('login');
        } else {
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            if ($this->products_model->login_model($username, $password)) {
                $sess_data = array(
                    'username' => $username,
                    'login_status' => '1'
                );
                $session_data = $this->session->set_userdata($sess_data);
                //write your code for redirect to dashboard
            }
        }
    }
}

答案 1 :(得分:1)

我更喜欢为仪表板创建另一个控制器,并在成功登录后将其重定向到那里。它很干净,你可以轻松做进一步的改变

if( $this->products_model->login_model($username,$password))
{
    $sess_data= array(
        'username'=> $username
    );

    $session_data   =   $this->session->set_userdata($sess_data);

    if(isset($session_data))
         redirect(site_url('/dashboard/'));

}

FM:$ sess_data不应该是单引号

答案 2 :(得分:1)

我将使用会话记录登录状态。

$session_data= array(
    'username'=> $username,
    'lgoin_status' => 1
);

$this->session->set_userdata('$session_data');

我会检查会话的login_status

if ($this->session->userdata('login_status')) {
    // do something
}

修改

在您的代码中,您应该首先检查会话,所以..

public function index()
{   
   if ($this->session->userdata('login_status')) 
   {
      // do something
   }
   else 
   {

   $data['products_data']= $this->products_model->all_products();  

   $this->form_validation->set_rules('username','Username','required');
   $this->form_validation->set_rules('password','Password','required');

   if ($this->form_validation->run()== false)
   {
       $this->load->view('login');           
   }
   ...
}

答案 3 :(得分:1)

删除

上的单引号

$this->session->set_userdata('$sess_data');

它将变量$sess_data视为常规字符串。

必须是:

$this->session->set_userdata($sess_data);