Question

How do you implement this in CodeIgniter?

Was it helpful?

Solution

(This answer was a response to the original question)

How would you do this ? Or, how would you create a session with a longer expiration date than the others ?

All created sessions would use the same $config['sess_expiration'] in the config file (default: 7200 seconds), is there a way to pass a custom value ?

Extending the expiration of a session cookie isn't going to work for a "remember me" feature, since the user will lose their session cookie when they close the browser. If Code Igniter doesn't have a native "remember feature", then you'll need to write something to drop a long-life cookie which contains information which will allow an automatic login when the server sees it again.

There's a good article on Persistent Login Cookie Best Practice, which can be summarized as:

  1. When the user successfully logs in with Remember Me checked, a login cookie is issued in addition to the standard session management cookie.2
  2. The login cookie contains the user's username and a random number (the "token" from here on) from a suitably large space. The username and token are stored as a pair in a database table.
  3. When a non-logged-in user visits the site and presents a login cookie, the username and token are looked up in the database. 1. If the pair is present, the user is considered authenticated. The used token is removed from the database. A new token is generated, stored in database with the username, and issued to the user via a new login cookie. 2. If the pair is not present, the login cookie is ignored.
  4. Users that are only authenticated via this mechanism are not permitted to access certain protected information or functions such as changing a password, viewing personally identifying information, or spending money. To perform those operations, the user must first successfully submit a normal username/password login form.
  5. Since this approach allows the user to have multiple remembered logins from different browsers or computers, a mechanism is provided for the user to erase all remembered logins in a single operation.

Another article which builds more security onto those ideas in Improved Persistent Login Cookie Best Practice

If you follow the practices in those articles, you won't go far wrong!

OTHER TIPS

You might want to reconsider the suggestion from 'Improved Persistent Login Cookie Best Practice'. I've posted my analysis of the problem on the site, but the gist of it is this:

You can not prevent cookie stealing without a secure line, no matter what

(*By 'secure line', I mean SSL / VPN tunneling / public-key encryption - or a robust challenge-response scheme *)

I'm all for persistent login best practices, but there's a point where no amount of trickery is going to help, and where the only way up is SSL.

Thanks for the solution. I have implemented this into my CI project.

DEV Note: the cookie should have a secured-hash as the remember-me key. - do not just store a user-id) - do not store a password, - and do not store hash of password. That way people can't automatically log into any account.

Worst case scenario: As soon as a hacker discovers the hash method, you can adjust the key producer function in one place and you are set up again with security. I would just adjust the salt and pepper strings.

Hope that helps. Chris D. - from Australia :)

For these things, the CI variables in all classes can be accessed by its own library:

For example:

$this->session->sess_expiration = 500000;

Hope that resolve your problem.

there is library on github for remember me download it.

Using this library's setCookie() function, the following occurs:

1.A cookie is generated containing a unique number This number is recorded into a database table, called ci_cookies along with the netid/username passed on to setCookie()

2.When the verifyCookie() function is called the browser's cookie hash must match the hash recorded to the database. If it does, verifyCookie() returns true forward it on home page

U need set conf before load session

$do_not_remember = (boolean) $this->input->post('do_not_remember');
if ($do_not_remember) {
   $this->config->set_item('sess_expiration', 0);
   $this->config->set_item('sess_expire_on_close', true);
}
$this->load->library('session');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top