Я пытаюсь реализовать функцию Remember me в CakePHP. Иногда (как и каждые 2-3 выхода), когда пользователь выходит из системы, он возвращает: незаконное смещение строки «имя пользователя» и «пароль» в строке 84 (помечено ниже в кодах AppController), в то время как отладка в строке 80 возвращает $ cookie для удаления ». Я не знаю, как проверить, работает ли файл cookie, но я полагаю, это означает, что это не так.CakePHP запомнить меня функция 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*&[email protected][email protected]*(XSL#$%)[email protected][email protected]#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
}
}
}
'debug ($ this-> Cookie-> read ('remember_me_cookie'))' и посмотреть, что в нем. –
Роджер, что. Он возвращает «deleted». Что-то не так с страницей входа? –
т. Е. 'Имя пользователя' и' пароль' не установлены. –