0

I am trying to implement remember me function in CakePHP. At times (like every 2-3 logouts) when a user logout it returns: illegal string offset 'username' and 'password' in line 84 (marked below in AppController codes), while debug in line 80 returns $cookie to be 'deleted'. I don't know how to check if the cookie is working but I presume this means it's not.

Users/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
        }
    }       
}
Alvin Mok
  • 323
  • 1
  • 14

1 Answers1

0

You can not store array in the cookie. You should serialize it. (And of corse you should not store password in cookie) But if I do not mistake you store only the username. Why would you read username and password as an array when there is only the username as a string?

Sigee
  • 362
  • 2
  • 11
  • I was reading this answered question http://stackoverflow.com/questions/12447487/cakephp-remember-me-with-auth and the solution seems working so – Alvin Mok Aug 10 '15 at 05:55
  • You store username as a string... "$this->Cookie->write('remember_me_cookie', $this->request->data['User'], true, '2 weeks');" Than you read it. "$cookie = $this->Cookie->read('remember_me_cookie');" Than you want use the readed string as an assiciative array. "'User.username' => $cookie['username']" – Sigee Aug 10 '15 at 06:05
  • So a string is not an array. – Sigee Aug 10 '15 at 06:07
  • Yes you are right. I edited the codes above to only save the username. but the illegal string problem still occurs. Could you be so kind to advise how the codes should be revised? – Alvin Mok Aug 10 '15 at 06:20
  • 1
    Simple use your $cookie variable without ['username'] index. – Sigee Aug 10 '15 at 06:23