php - CakePHP 使用 Auth 记住我

标签 php session cakephp authentication cookies

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

最佳答案

在你的用户 Controller 中:

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

在登录 View 中:

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

在您的 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
        }
    }
}

关于php - CakePHP 使用 Auth 记住我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12447487/

相关文章:

php - PHPMyAdmin 中没有结果

PHP:如何将 "near white"表示为颜色?

php - 如果已通过身份验证并且用户存在于数据库中,则将 Facebook 用户登录到 CakePHP 应用程序

php - CakePHP 2.1 初学者教程?

php - SimpleXMLElement 在 addChild 和 addAttribute 中处理文本值的基本原理

php - php 中格式错误的 unicode/十六进制

java - 跨请求将 HashMap Cookie 存储库存储在 Servlet 中

session - 在Tensorflow中,Session.partial_run和Session.run有什么区别?

node.js - cookie-session中间件在expressjs中如何工作?

mysql - 当我通过 Asc Cakephp 2.0 应用订单时,在顶部添加了空值