使用CodeIgniter的会话似乎不起作用

时间:2015-06-25 03:55:13

标签: php codeigniter session

这是我的代码登录。

  //check if username and password is correct
                    $usr_result = $this->signin_model->get_user($username, $password);
                    if ($usr_result > 0) //active user record is present
                    {
                         //set the session variables
                         $sessiondata = array(
                              'username' => $username,
                              'loginuser' => TRUE
                         );
                         $this->session->set_userdata('logged_in', $sessiondata);
                         redirect("application");
                    }
                    else

这是我的退出代码。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Logout extends CI_Controller {

     public function __construct()
     {
          parent::__construct();

          $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
    $this->output->set_header("Pragma: no-cache");
    $this->load->library('session');
     }

      function index()
         {
         $this->session->unset_userdata('logged_in');
         $this->session->sess_destroy();
         redirect('signin', 'refresh');

         }

这是我的控制器

public function __construct()
     {
           parent::__construct();
               $this->load->library('session');

                $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
        $this->output->set_header("Pragma: no-cache"); 

         if($this->session->userdata('logged_in'))
           {
               $session_data = $this->session->userdata('logged_in');
               $this->load->helper('form');
                   $this->load->helper('url');
                   $this->load->helper('html');
                   $this->load->database();
                   $this->load->library('form_validation');
               $this->load->library('javascript'); 
               $this->load->library('javascript/jquery');
               $this->load->model('chan_model');
           }
           else
           {
             //If no session, redirect to login page
             echo "Chnnlll";
             exit;
             redirect('signin', 'refresh');
           }
     }

现在,问题是,我正在登录时创建会话,但是当我退出时我将其销毁,我也尝试在我的所有控制器文件中清除缓存,但它似乎不起作用。我现在已经测试了很长一段时间,但我不确定这个问题到底是什么。

虽然我的注销代码很好,但有些方法可以让我再次访问控制器页面,但不应该,我认为这可能是一些缓存问题,但现在也要清除它。

请让我知道哪个是我出错了。

1 个答案:

答案 0 :(得分:0)

<?php
    //set session user
    $username = $_POST['username'];
    $password = $_POST['password'];

    $usr_result = $this->signin_model->get_user($username, $password);

    if(!empty($usr_result)) //active user record is present
    {
        //set the session variables
        $sessiondata = array(
            'username' => $username,
            'logged_in' => TRUE
        );
        $this->session->set_userdata($sessiondata);//alter this line
        redirect("application");
    }
    else
    {

    }

每个控制器日志之前,检查会话用户是否设置

  

在每个控制器粘贴此

在控制器中

    $result = $this->signin_model->log_in();
    if($result==true)
    {
        //if TRUE only allow user to access page
    }
    else
    {
        //redirect to Logging page
    }

在模型中

public function log_in()
{
    $log = $this->session->all_userdata();

    if (isset($log['username'])) {
        return true;
    } else {
        return false;
    }
}

Ci Session Helper