Irfan Ali
Irfan Ali

Reputation: 11

How to keep cookies from expiring even after browser restart in CakePHP application

I have en e-commerce cakephp (v 1.3) app where customers can add stuff to a cart. As long as the user doesn't exist the browser or the session timeout period isn't exceeded, the session variables defining the cart contents stays stored. But if they close the browser (IE/Firefox) the cart gets empty since the cookie gets destroyed. Oddly enough, Chrome doesn't have this problem which is another mystery.

I've tried to change the Session.cookieTimeout variable in the /app/config/core.php file but that doesn't have any effect. Whenever I pull up the cookie information on the browser dev tools it shows the cookie name "PHPSESSID" and expiration keeps showing "when the session ends".

I've also tried overwriting cookie expiration time in the AppController beforeFilter() function with $this->Cookie-$time but that didn't do anything either to define the session cookie timeout.

My guess is some setting keeps the cookie timeout set to 0 which is the default setting for session ends when browser closes.

What other things should I try to keep the cookies from getting destroyed when a user closes the browser?

Upvotes: 1

Views: 1690

Answers (1)

Harish Ambady
Harish Ambady

Reputation: 13141

update

session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means "until the browser is closed." Defaults to 0. See also session_get_cookie_params() and session_set_cookie_params().

I think setting your session.cookie_lifetime to such a high value will mean you will have many stale sessions on your server and this might be a issue if you have large numbers of sessions - typically sessions are stored in the system's /tmp folder. When this folder, typically on *nix machines, fills up it could cause problems on the machine - processes start locking up as they fail to create temporary files for whatever reason. (You can change the session.save_path to something other than /tmp so you don't have this issue - especially on shared hosts).

And also set this in .htaccess file rather than php.ini so that it won't affect your other projects.

Upvotes: 1

Related Questions