What are the best practices for dealing with session timeout in PHP applications?
Session timeout in PHP applications can be managed by setting the session.gc_maxlifetime value in the php.ini file to the desired timeout period. Additionally, you can also implement a custom session timeout mechanism in your PHP code by checking the last activity time of the user and expiring the session if it has been inactive for a specific period.
// Set session timeout to 30 minutes
ini_set('session.gc_maxlifetime', 1800);
// Start the session
session_start();
// Check if last activity time is set
if(isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > 1800)) {
// Expire the session if inactive for more than 30 minutes
session_unset();
session_destroy();
}
// Update last activity time
$_SESSION['last_activity'] = time();