Search code examples
phpsessioncakephpremember-me

how to send session's content to a remember me cookie?


I am working on remember me cookie in cakephp which keeps you logged in for 2 weeks.

It is working fine, but what I am passing to be written onto the cookie:

$this->request->data['User']['username']

Whenever a new user is registered, a hashed ID is created by default.

The remember me cookie just generates a key which checks if its the same user and logins in the backgroud. When the cookie created by cakephp expires (default being four hours), all the information such as user ID, address, etc is lost.

How do I send the ID to be checked instead?

How to I store all of those(user info) even after four hours? (say for around 2 weeks)


Solution

  • Change your login function like this

     function login() {
        if ($this->Auth->user()) {
            if (!empty($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, '+2 weeks');
                unset($this->data['User']['remember_me']);
            }
            $this->redirect($this->Auth->redirect());
        }
        if (empty($this->data)) {
            $cookie = $this->Cookie->read('Auth.User');
            if (!is_null($cookie)) {
                if ($this->Auth->login($cookie)) {
                    //  Clear auth message, just in case we use it.
                    $this->Session->delete('Message.auth');
                    $this->redirect($this->Auth->redirect());
                }
            }
        }
    }