CakePHP记得我的cookie函数返回Illegal string offset

时间:2015-08-10 05:43:55

标签: php cakephp cookies

我正在尝试在CakePHP中实现记住我的功能。有时(如每2-3次注销)当用户注销时返回:非法字符串偏移'用户名'和密码'在第84行(在AppController代码中标记为以下),而第80行中的调试返回$ cookie以便删除'。我不知道如何检查cookie是否有效,但我认为这意味着它没有。

用户/ login.ctp:

<?php 
echo $this->Form->create('User', array(
    'url' => array(
    'controller' => 'users', 
    'action' => 'login'),
    'class' => 'form-signin', 'inputDefaults' => array(
        'label' => false, 'div' => false)
));

echo $this->Form->input('User.username', array(
    'placeholder' => 'username', 
    'class' => 'form-control'
)); 

echo $this->Form->input('User.password', array(
    'placeholder' => 'password', 
    'class' => 'form-control', 
    'type' => 'password'
    )); 
?>
<?php echo $this->Form->checkbox('User.remember_me', array('label' => 'remember me')); ?>
<?php echo $this->Form->label('User.remember_me', 'Remember Me'); ?>

<?php
$options = array(
    'label' => 'Login',
    'class' => 'btn btn-default', 
    'div' => array(
    'class' => 'form-group')
);
echo $this->Form->end($options);
?>

UsersController:

public function login() {

if ($this->Session->read('Auth.User')) {
    $this->Session->setFlash(__('You are already logged in!'), 'alert_box', array('class' => 'alert-warning'));
        return $this->redirect($this->referer());
}

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

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

        $this->Session->setFlash(__('You are logged in!'), 'alert_box', array('class' => 'alert-success'));
        if ($this->Session->read('lastUrl')) {
            return $this->redirect($this->Session->read('lastUrl'));
        } else {
            return $this->redirect($this->Auth->redirect());
        }
    }
    $this->Session->setFlash(__('Your username or password was incorrect.'), 'alert_box', array('class' => 'alert-danger'));
}
}

AppController:

public $components = array(
    'Acl',
    'Auth' => array(
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        ),
        'unauthorizedRedirect' => '/posts'
    ),
    'Session',
    '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');
        $this->loadModel('User');
        $user = $this->User->find('first', array(
            'conditions' => array(
                'User.username' => $cookie['username'] //line 84
            )
        )); 
        if ($user && !$this->Auth->login($user['User']['username'])) {
            $this->redirect('/users/logout'); // Destroy Session & Cookie
        }
    }       
}

1 个答案:

答案 0 :(得分:0)

您无法在Cookie中存储数组。你应该序列化它。 (并且你不应该在密码中存储密码)但是如果我没有弄错你只存储用户名。当只有用户名作为字符串时,为什么要将用户名和密码作为数组读取?