Checking the lifecycle of a PHP 5.1 login session
A login session connects successive requests to server-side authentication state. Treat its identifier as a credential: anyone able to present a valid authenticated identifier may be able to use the same account.
Keep identifiers out of links
Configure session.use_only_cookies and disable transparent session-ID rewriting for the application. Set cookie parameters before session_start and choose the narrowest useful path and domain. Keep the authenticated area on HTTPS and set the Secure cookie flag. A secure login form followed by authenticated pages over ordinary HTTP leaves the session exposed in transit.
Create a new identifier after authentication
Regenerate the session identifier when login succeeds, before assigning authenticated privileges to the new session. Check for failure rather than continuing with an unchanged identifier. Ensure an earlier identifier cannot acquire the new authentication state. Test multiple tabs and interrupted requests, and store session files in a protected location outside the document root.
Enforce expiry and logout in the application
Record activity and enforce an idle limit on the server instead of relying solely on cookie expiry or probabilistic garbage collection. Logout should clear the session state, destroy its stored record and expire the cookie using the same path and domain settings. Check authorisation on every protected request. Try reusing the old identifier after logout to verify that it no longer grants access.
Use two independent browser sessions to check login, expiry and logout. Avoid recording complete session identifiers in routine access or debugging logs.
References
PHP manual: session_regenerate_id