使用代码编辑器中的cookie记住登录名复选框

问题描述 投票:0回答:1

我想在单击“记住我”复选框时保存电子邮件和密码,并且cookie应该设置为“记住我”。登录正常。请帮助我处理我在codeignitor中的代码,这是我的控制器代码:

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



        }

下面是我实现的Cookie代码:

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

   }
php codeigniter session-cookies remember-me
1个回答
0
投票

首先,您必须包括我在评论部分中提到的cookie助手

此后在您的控制器中

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');
             }
    }
© www.soinside.com 2019 - 2024. All rights reserved.