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();