Home >Backend Development >PHP Tutorial >How Can I Reliably Implement a 30-Minute PHP Session Expiration?
Problem Statement:
Maintaining an active session for 30 minutes before terminating it.
Answer:
Relying solely on PHP's built-in session settings (e.g., session.gc_maxlifetime, session.cookie_lifetime) may not guarantee reliable session expiration. Instead, it's recommended to implement a custom expiration mechanism.
session.gc_maxlifetime:
session.cookie_lifetime:
Custom Expiration Handler:
To ensure reliable session expiration, implement a custom expiration handler that uses a timestamp to track user activity.
Code:
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) { // Session inactive for over 30 minutes session_unset(); // Delete session data session_destroy(); // Terminate session } $_SESSION['LAST_ACTIVITY'] = time(); // Update timestamp
Additional Considerations:
The above is the detailed content of How Can I Reliably Implement a 30-Minute PHP Session Expiration?. For more information, please follow other related articles on the PHP Chinese website!