Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the expiry time of PHPSESSID Cookie

Tags:

php

cookies

I have been at this for a day now, but nothing seems to be working. What I want to do: change the expiry time of the session cookie PHPSESSID, when a particular checkbox is checked , how do I do this ? I have tried:

ini_set()

session_set_cookie_params()

setcookie()

but nothing works . Can someone please please help me here ?

Thanks

like image 741
gyaani_guy Avatar asked Jan 01 '11 14:01

gyaani_guy


1 Answers

To specify the session lifetime, server side, either apply the following command

  ini_set('session.gc_maxlifetime', 30*60); // expires in 30 minutes

or set it in your php.ini file.

To set the session cookie lifetime, client side, either let it as it is (0, will die when the browser is closed), or

  ini_set('session.cookie_lifetime', 30*60); // 30 minutes

or in the php.ini.

If you choose to use ini_set(), be sure to place the commands before session_start() is called.

Note that the ini_set function sets configuration option(s) during the script execution time only.

Regarding the checkbox and having a dynamic setting of the session lifetime, you could

  • use APC to store a setting shared by all PHP processes, that will last until the PHP server is down
  • write a value in a file somewhere that you load at the start of scripts (expensive) and set the value

(each script will have to ini_set() once before session_start())

like image 182
Déjà vu Avatar answered Sep 18 '22 18:09

Déjà vu