Search code examples
phpcodeignitersession-cookiesremember-me

remember me checkbox in login using cookie in codeignitor


i want to save email and password on clicking remember me checkbox and cookie should get set on remember me.login is working fine. kindly help me with my code in codeignitor here is my controller code:

       public function loginaction()
        {

       $email=$this->input->post('email');
            $password=$this->input->post('password');
            $where = array('email'=>$email,'password'=>$password);
            $tbname='login';
            $query = $this->Insert_Model->viewdata($tbname,$where);

                    if(empty($query))
                    {
                        $data['msg']="Invalid email or password";
                        $this->load->view('login',$data);
                    } 
                    else 
                    {
                        redirect('dashboardv1');
                    }



        }

below is cookie code which i implemented:

function set()

   {

       $cookie= array(

           'name'   => 'chkremember',
           'value'  => 'test',                            
           'expire' => '300',                                                                                   
           'secure' => TRUE

       );

       $this->input->set_cookie($cookie);



   }



   function get()

   {

       echo $this->input->cookie('chkremember',true);

   }

Solution

  • firstly you have to include cookie helper as I mention in the comment section

    After that in your controller

    public function loginaction()
        {
            $this->load->helper('cookie');
            $email=$this->input->post('email');
            $password=$this->input->post('password');
            $where = array('email'=>$email,'password'=>$password);
            $tbname='login';
            $query = $this->Insert_Model->viewdata($tbname,$where);
    
                    if(empty($query))
                    {
                        $data['msg']="Invalid email or password";
                        $this->load->view('login',$data);
                    } 
                    else 
                    {
                 //first you have to delete old cookie and create new one
                    delete_cookie("email");
                    delete_cookie("password");
                    if ($this->input->post('remember') == 'true') {
    
                      $userName = array(
                        'name' => 'email',
                        'value' => YOUREMAIL,
                        'expire' => '86500',
                        'prefix' => '',
                        'secure' => FALSE
                      );
                      $this->input->set_cookie($userName);
    
                      $password = array(
                        'name' => 'password',
                        'value' => YOURPASSWORD,
                        'expire' => '86500',
                        'prefix' => '',
                        'secure' => FALSE
                      );
                      $this->input->set_cookie($password);
                    }
                        redirect('dashboardv1');
                 }
        }
    

    Get the cookie you can use below code

    <?php echo get_cookie('email'); ?>
    <?php echo get_cookie('password'); ?>