Are there any best practices for handling session regeneration and expiration in PHP?
Session regeneration and expiration are important for security in PHP applications to prevent session fixation and session hijacking attacks. It is recommended to regenerate the session ID periodically or after a certain number of requests to prevent session fixation. Additionally, setting a reasonable session expiration time helps to limit the window of opportunity for attackers to hijack a session.
// Session regeneration
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
session_regenerate_id(true);
$_SESSION['LAST_ACTIVITY'] = time();
}
// Session expiration
if (isset($_SESSION['CREATED']) && (time() - $_SESSION['CREATED'] > 3600)) {
session_unset();
session_destroy();
}