What is the best practice for setting session timers in PHP to automatically close sessions after a certain period of time?
To automatically close sessions after a certain period of time in PHP, it is best practice to set a session timeout value in the php.ini file or in the code using the session.gc_maxlifetime directive. This directive sets the maximum number of seconds a session may be active. Additionally, you can also use session_set_cookie_params() function to set the session cookie expiration time.
// Set session timeout to 30 minutes
ini_set('session.gc_maxlifetime', 1800);
// Set session cookie expiration time
session_set_cookie_params(1800);
// Start the session
session_start();
Related Questions
- How can PHP be utilized to redirect error messages to the sender's email address in case of mail delivery failures?
- What are common pitfalls when combining HTML and PHP in a single document?
- How can PHP be optimized to efficiently handle the continuous transfer and display of webcam images from multiple sources?