1

我正在尝试设置它,以便当用户登录时有一个记住我的复选框,如果选中该复选框,则会写入一个 cookie 以记住用户的电子邮件和密码。在我的用户控制器登录功能中是这样的:

public function login() {
    if (!empty($this->data)) {
        if ($this->Auth->login()) {
            $userId = $this->Auth->user('id');
            if(!empty($this->data['User']['remember'])) {
                $user = $this->User->find('first', array('conditions'=>array('id'=>$userId), 'recursive'=>-1, 'fields'=>array('email', 'password')));
                $this->Cookie->write('User', $user['User']);
            }
... etc etc ...

在 cakephp 1.x 中,我有这个工作,所以在我的 AppController 的 beforefilter 中,我只需查找 cookie 并尝试像这样登录:

//try to auto login a users
    if($this->Auth->user() == null) {
        $user = $this->Cookie->read('User');
        if(!empty($user)) {
            $this->Auth->login($user);
        }
    }

但这现在似乎不起作用。我认为因为从我读到的内容中,如果您将任何内容传递给登录函数,它将返回 true。为了正确登录,我需要将该cookie的内容发布到登录功能......

那正确吗?有没有比尝试在某处创建表单并通过大量重定向发布信息更简单的方法?

我还尝试将 cookie 信息添加到 $this->request->data 数组中并尝试登录,但这也不起作用:(

//try to auto login a users
if($this->Auth->user() == null) {
    $user = $this->Cookie->read('User');
        if(!empty($user)) {
            $this->request->data['User']['email'] = $user['email'];
            $this->request->data['User']['password'] = $user['password'];
            $this->Auth->login();
        }
    }

谁能帮我吗?必须有一个更简单的方法来做到这一点!

4

1 回答 1

1

尝试这个..

将此复选框保留在登录 ctp 文件中

 <php echo $this->Form->checkbox('remember_me',array("id" => "id_remember_me",'name'=>'remember_me','label' => false)); ?>
 <span onclick="$('#id_remember_me').attr('checked',true);"><?php echo __('Remember my details')?></span>

然后保持这个

code in the controller where the $this->Auth->login() is..
     //Setting up the expiry time
     $year = time() + 31536000;
     if($this->request->data['remember_me']) {
     // Creating a cookie in the name of remember_me with the username for the time which was set
          setcookie('remember_me', $this->request->data['User']['username'], $year);
     }
     else if(!$this->request->data['remember_me']) {
         if(isset($_COOKIE['remember_me'])) {
              $past = time() - 100;
              setcookie('remember_me', 'gone', $past);
         }
     }
于 2013-04-06T07:17:26.897 回答