记住我使用codeigniter

时间:2014-10-01 14:01:24

标签: codeigniter remember-me

嗨,我有这个在我的网站上记住我,我想要这个工作。我注意到我的代码不起作用。下面是我的代码

public function __construct(){
    parent::__construct();
    $this->load->model('login_model', 'lm');
    $this->load->model('users_model', 'um');
    if($this->input->post('remember_me')){
      $this->config->set_item('sess_expire_on_close', '0');
      //echo $this->input->post( 'remember_me' ); exit;
    }
    if($this->session->userdata('loggedIn')){
      redirect('profile');
    }
  }

我的config.php文件

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
#$config['sess_expire_on_close']    = FALSE;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = FALSE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

我已经将sess_expire_on_close代码设置为TRUE 有人能给我一些更好的代码方式记住我吗? 任何帮助都非常值得感谢

1 个答案:

答案 0 :(得分:0)

我建议将$this->load->library('session');放入构造函数中 尝试这样的事情:

public function __construct(){
    parent::__construct();
    $this->load->library('session');
    $this->load->model('login_model', 'lm');
    $this->load->model('users_model', 'um');
    if($this->input->post('remember_me')){
            $this->session->set_userdata(''); // Here put what you want to save
            redirect('somewhere,'refresh');
    }
    if($this->session->userdata('loggedIn')){
      redirect('yourlink/controller/logout');
    }
  }
function logout(){ //Function for logging out
        $this->session->sess_destroy();
        redirect('documents/index','refresh');
    }

我还建议检查用户是否属于特定类型,例如管理员,主持人,用户等。这是如果你有一个登录系统,那么你可能想做这样的检查:

$userType = $this->yourmodel->checkuser($this->input->post('username'),$this->input->post('password'));
if($userType == 1 || $userType == 2)
{
$this->session->set_userdata('userType');
redirect('somewhere','refresh');
...
}

希望这可以帮助你:)

干杯