1

Codeigniter session ends after i close browser how to make remember checkbox ?

checkbox already in login form but i don't know how to do it ?

here's the login method in controller

    public function login($page = 'login') {
        if (!file_exists(APPPATH. '/views/main/' .$page. '.php')) {
            // Whoops, we don't have a page for that!

            show_404();
        }
                if($this->session->userdata('is_logged_in')) {
        redirect('home');
        $this ->session ->set_flashdata('alreadysignedin', '<div class="alert alert-info"><strong>Srroy !</strong> but you are already signed in.</div>');
    } else {
        $this ->load ->library('form_validation');
        $data['title'] = ucfirst($page); // Capitalize the first letter
        $this ->load ->model('users_model');
        $this ->load ->view('include/header', $data);
        $username = $this->input->post('username');
// Get User Role 
    $this->load->model('user_profile');
    $this->user_profile->rolesdetail($username);
    $userrole = $this->user_profile->rolesdetail($username);
// Get User Role </>
        if ($this ->input ->post('login') AND $this->form_validation ->run('login') == TRUE) {
            $data = array(
                'id'             => $userrole->id,
                'username'       => $this ->input ->post('username'),
                'is_logged_in'   => TRUE,
                'roles'          => $userrole->roles,
                'email'          => $userrole->email
            );
            $this ->session ->set_userdata($data);
            $this ->session ->set_flashdata('logindone', '<div class="alert alert-success"><strong>Congratulation!</strong> you have logged in successfully.</div>');
            redirect('home');
        }
        $this ->load ->view('main/'.$page, $data);
        $this ->load ->view('include/footer', $data);
        }
    }

i read about cookies but i did not known how it works

AhmedEls
  • 708
  • 2
  • 9
  • 25
  • Please check these posts - http://stackoverflow.com/questions/19896237/codeigniter-setting-remember-me-in-sessions and http://www.formget.com/codeigniter-remember-me-checkbox/ – VishwaKumar Nov 26 '15 at 08:55
  • http://stackoverflow.com/questions/14359806/why-codeignitor-does-not-finish-the-session-after-closing-the-browser – Zeeshan Nov 26 '15 at 09:20

1 Answers1

6

It's simple, after submitting the form while verifying/processing the user credentials, if the checkbox value is found then create a cookie using $this->input->set_cookie(); after loading the cookie helper. Store the login info in that cookie and when the user arrives on login page check for that cookie and if data is present in that cookie just populate the form and its done. You can also keep the cookie data encrypted. check this link for further codeigniter cookies

Sandeep J Patel
  • 910
  • 9
  • 24