CakePHP用Auth记住了我

goo*_*ing 14 php authentication cookies session cakephp

我已经成功使用了Auth,但遗憾的是,它似乎只适用于Session.我希望如果用户选中"记住我"复选框,我会使用Cookie,他将登录2周.我在官方书籍和谷歌中找不到任何东西我发现只有少数而不是很棒的博客文章.有没有办法在不重写核心的情况下实现这一点?

Hof*_*off 48

在您的用户控制器中:

public function beforeFilter() {
    $this->Auth->allow(array('login', 'register'));
    parent::beforeFilter();
}

public function login() {
    if ($this->request->is('post')) {

        if ($this->Auth->login()) {

            // did they select the remember me checkbox?
            if ($this->request->data['User']['remember_me'] == 1) {
                // remove "remember me checkbox"
                unset($this->request->data['User']['remember_me']);

                // hash the user's password
                $this->request->data['User']['password'] = $this->Auth->password($this->request->data['User']['password']);

                // write the cookie
                $this->Cookie->write('remember_me_cookie', $this->request->data['User'], true, '2 weeks');
            }

            return $this->redirect($this->Auth->redirect());

        } else {
            $this->Session->setFlash(__('Username or password is incorrect.'));
        }
    }

    $this->set(array(
        'title_for_layout' => 'Login'
    ));
}

public function logout() {
    // clear the cookie (if it exists) when logging out
    $this->Cookie->delete('remember_me_cookie');

    return $this->redirect($this->Auth->logout());
}
Run Code Online (Sandbox Code Playgroud)

在登录视图中:

<h1>Login</h1>

<?php echo $this->Form->create('User'); ?>
    <?php echo $this->Form->input('username'); ?>
    <?php echo $this->Form->input('password'); ?>
    <?php echo $this->Form->checkbox('remember_me'); ?> Remember Me
<?php echo $this->Form->end('Login'); ?>
Run Code Online (Sandbox Code Playgroud)

在AppController中:

public $components = array(
    'Session',
    'Auth',
    'Cookie'
);

public $uses = array('User');

public function beforeFilter() {
    // set cookie options
    $this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
    $this->Cookie->httpOnly = true;

    if (!$this->Auth->loggedIn() && $this->Cookie->read('remember_me_cookie')) {
        $cookie = $this->Cookie->read('remember_me_cookie');

        $user = $this->User->find('first', array(
            'conditions' => array(
                'User.username' => $cookie['username'],
                'User.password' => $cookie['password']
            )
        ));

        if ($user && !$this->Auth->login($user['User'])) {
            $this->redirect('/users/logout'); // destroy session & cookie
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 使用上述密码的另一个好处是,如果您更改密码,它会自动使所有设备上的cookie无效,从而强制重新登录.:) (4认同)
  • 知道我的密码,即使是加密的,也会在cookie中的某个地方浮动,我会感到非常不舒服.我认为在这种情况下,存储用户名就足够了. (3认同)
  • 看起来他似乎没有兴趣获得太多的帮助,但是这些人花时间去寻求帮助是很遗憾的.我记得将来要避开@hey的问题. (2认同)
  • 谢谢你的详细解答!我在一个应用程序中使用了这个,但我相信"$ this-> Auth-> login($ user)"应该是"$ this-> Auth-> login($ user ['User'])".现在它完美无缺! (2认同)

Abi*_*ain 6

看到这个URL我觉得这对你很有帮助.

http://lecterror.com/articles/view/cakephp-and-the-infamous-remember-me-cookie

或试试这个

function login() {
    if ($this->Auth->user()) {
        if (!empty($this->data) && $this->data['User']['remember_me']) {
            $cookie = array();
            $cookie['username'] = $this->data['User']['username'];
            $cookie['password'] = $this->data['User']['password'];
            $this->Cookie->write('Auth.User', $cookie, true, COOKIE_EXPIRE);
            unset($this->data['User']['remember_me']);
        }

        $this->LogDetail->Write('activity','has logged IN');
        $this->redirect($this->Auth->redirect());
    }

    if (empty($this->data)) {
        $cookie = $this->Cookie->read('Auth.User');
        if (!is_null($cookie)) {
            if ($this->Auth->login($cookie)) {
                $this->Session->destroy('Message.Auth'); # clear auth message, just in case we use it.
                $this->LogDetail->Write('activity','has been authenticated via cookie and is now logged IN');

                $this->redirect($this->Auth->redirect());
            } else {
                $this->LogDetail->Write('activity','attempted to gain access with an invalid cookie');
                $this->Cookie->destroy('Auth.User'); # delete invalid cookie

                $this->Session->setFlash('Invalid cookie');
                $this->redirect('login');
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 知道我的密码,即使是加密的,也会在cookie中的某个地方浮动,我会感到非常不舒服.我认为在这种情况下,存储用户名就足够了. (8认同)

sib*_*iba 3

记住我只不过是用 cookie 标识的会话,但 cookie 的生存期设置为无穷大。查看 Config/core.php 以了解会话 cookie 的生命周期。

  • Cookie 的寿命还不够。您还必须使会话在服务器上持续那么长时间。 (3认同)