Session lifetime in PHP

By default, the session lifetime is only 24 minutes, and cookies are stored until the browser is closed, such parameters are set on many hosting services.

Default PHP session settings

This is not acceptable if you have an online store, because a customer can put products in the cart and return to the site in an hour or more, and the cart is already empty...

Time can be changed in .htaccess

php_value session.gc_maxlifetime 172800
php_value session.cookie_lifetime 172800
htaccess

Or PHP function ini_set()

ini_set('session.gc_maxlifetime', 172800);
ini_set('session.cookie_lifetime', 172800);
PHP

In php.ini

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
; http://php.net/session.gc-maxlifetime
session.gc_maxlifetime = 172800

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
; http://php.net/session.cookie-lifetime
session.cookie_lifetime = 172800
  • 1 day = 86400 seconds
  • 2 days = 172800 seconds
  • 3 days = 259200 seconds
  • 1 week = 604800 seconds
01.11.2017, updated 20.02.2021
150

Comments

to add a comment.

Other publications

Regular Expression Shortcodes
Often on sites, it becomes necessary to insert dynamic information blocks into the texts of pages - banners, phones...
25
0
Checking data with regular expressions
A collection of regular expressions with examples in PHP for validating data from form fields.
49
0
Serialize function, possible problems
The serialize () function returns a string representation of any value (array, object, etc.). Using serialize on an...
32
0
Examples of using cURL in PHP
cURL PHP is a library for receiving and transmitting data via protocols such as HTTP, FTP, HTTPS. The library is used...
329
0
How to set up Last-Modified
The Last-Modified header helps to optimize the loading of web pages and make the job easier for search engines.
100
0
How to find out if a request came via AJAX
To distinguish between direct and AJAX requests, simply check the value of the $ _SERVER global variable
60
0