0

Hi i have this remember me in my website and i want that to work. Ive noticed that my code does not work. Heres my code below

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');
    }
  }

my config.php file

$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;

i already set the sess_expire_on_close code to TRUE can someone gave me a little more nicer way of code on remember me part? any help is muchly appreicated thanks

hello
  • 81
  • 1
  • 12

1 Answers1

0

I suggest placing $this->load->library('session'); into your constructor Try something like this:

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');
    }

I also suggest to check if the user belongs to a specific type, for example admin, moderator, user etc. This is if you have a login system, then you might wanna do the check like this:

$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');
...
}

Hope this help you :)

Cheers

John M.
  • 76
  • 1
  • 4
  • 11
  • You need to save the remember me value somewhere so that you can use it in the session. The code needs a little modifying, it's working in my case. – John M. Oct 04 '14 at 14:20
  • so what needs to be set_userdata in my case? the username? – hello Oct 05 '14 at 03:09