PHP session expiration timeout for a specific number of minutes / hours / days

I have a website that creates a shopping cart session.

$_SESSION['cart']=array(); 

It seems that the server automatically kills the session after an inactivity X-time, I assume it is set in php.ini (but my host does not give me access, they just let me inform them of the changes, so I can not play around! :( )

Is there a better way to keep sessions alive, for example, for 2 days or for a specific number of minutes / hours?

+7
source share
2 answers

Call session_set_cookie_params() before calling session_start() in your scripts:

 $session_lifetime = 3600 * 24 * 2; // 2 days session_set_cookie_params ($session_lifetime); session_start(); // ... 

From the documentation :

session_set_cookie_params()
Set cookie options defined in php.ini file. The effect of this function is saved only for the duration of the script. Thus, you need to call session_set_cookie_params() for each request and before calling session_start() .

Alternatively, you can update the directive of your php.ini session.cookie_lifetime file to 2 days (in seconds).

+13
source

set a cookie and change in your config that the session uses cookies

 session_start(); set_cookie("PHPSESSID", session_id(), time() + 3600 * 2); 

this will continue for 2 hours.

+1
source

All Articles