Engineering 4 min read Updated
Sessions and cookies done safely
In short
Protect session cookies with explicit attributes, a new ID at login and timeouts enforced on the server.
A session cookie is proof that a browser is logged in, so anyone who obtains it can act as that user. The defences are simple to describe and easy to get wrong. Set the cookie’s attributes explicitly, change the session identifier at login, limit how long a session lives, and end it properly on logout. The sources behind this are the OWASP Session Management Cheat Sheet, the MDN reference for Set-Cookie and the PHP manual.
Set the cookie attributes explicitly
Two attributes do most of the work. Secure tells the browser to send the cookie only over HTTPS. MDN notes that browsers make an exception for localhost. HttpOnly stops JavaScript from reading the cookie through document.cookie, which limits the damage from a cross-site scripting bug. It does not stop the browser from sending the cookie with fetch or XMLHttpRequest requests. OWASP treats both attributes as mandatory for session cookies.
In PHP, session_set_cookie_params accepts options for lifetime, path, domain, secure, httponly and samesite. Call it before session_start, and on every request, because its effect lasts only for the current script.
SameSite and requests from other sites
SameSite controls whether a cookie is sent with requests that come from another site. Strict sends the cookie only for requests from the same site. Lax also sends it on cross-site top-level navigations that use a safe method, which excludes POST, PUT and DELETE. None sends it everywhere, and it must be paired with Secure. OWASP prefers Strict for session cookies and accepts Lax. Browsers differ in what they do when the attribute is missing, so set it explicitly. SameSite is one layer against forged requests, not the only one. See CSRF protection in plain terms.
Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea
Change the session ID at login
OWASP describes regeneration during authentication as the most common scenario in which it is mandatory. The reason is session fixation. If an attacker can make a victim use a session ID the attacker already knows, and that ID survives the login, the attacker shares the logged-in session. Issue a new ID when the user logs in.
In PHP, session_regenerate_id replaces the current ID and keeps the session data. Its delete_old_session argument defaults to false. The PHP manual advises against deleting the old session, because doing so disables detection and prevention of session hijacking. Keep the default unless you have a specific reason to change it.
Set lifetimes and end sessions on the server
OWASP gives idle timeouts of two to five minutes for high-value applications, and 15 to 30 minutes for low-risk ones. For absolute timeouts it offers an example of four to eight hours for an office worker who uses the application all day. Choose values for the risk of your application, not for convenience alone.
Logout must invalidate the session on the server. OWASP also says that all session IDs from the same authenticated session must be revoked together, including IDs from earlier rotations that are still in their grace period. A cookie that expires in the browser is not the same as a server-side expiry. MDN explains that a cookie without Expires or Max-Age is a session cookie, which is removed when the browser shuts down. The server must therefore enforce its own timeout.
What to check in your session setup
Check each item against the live site:
- Set Secure, HttpOnly and SameSite explicitly, and confirm them in the Set-Cookie header in your browser’s developer tools.
- Regenerate the session ID after every successful login.
- Set idle and absolute timeouts for the risk of the application, and enforce them on the server.
- Invalidate the server-side session on logout, and revoke related IDs with it.
- Keep session identifiers out of URLs and logs. This is general practice, not a rule quoted from these sources.
Session security depends on the login behind it. The guidance on storing passwords correctly and on two-factor authentication for admin access covers the other half of the problem.
This article draws on the OWASP cheat sheet, the MDN Set-Cookie reference and the PHP manual pages for session_set_cookie_params and session_regenerate_id. It is general information, not a security audit of your application.
Common questions
- Does a session cookie have to use HTTPS?
- OWASP says the Secure attribute must be used so that the session ID is exchanged only over an encrypted channel. MDN notes that browsers make an exception for localhost.
- What does SameSite change for requests from another site?
- With Strict, the cookie is sent only for requests from the same site. With Lax, it is also sent on cross-site top-level navigations that use a safe method, which excludes POST, PUT and DELETE.
- How long can an idle session stay open?
- OWASP gives idle timeouts of two to five minutes for high-value applications and 15 to 30 minutes for low-risk ones. For absolute timeouts it gives an example of four to eight hours for an office worker using the application all day.
Sources
- Session Management Cheat Sheet, OWASP Foundation. Accessed 24 November 2026
- Set-Cookie header, MDN Web Docs (Mozilla). Accessed 24 November 2026
- session_set_cookie_params, PHP Group (php.net). Accessed 24 November 2026
- session_regenerate_id, PHP Group (php.net). Accessed 24 November 2026
Further reading
-
CSRF protection in plain terms
Every state-changing request needs its own token; SameSite and an Origin check add layers of defence, but they do not replace the token.
-
Storing passwords correctly in PHP
Store passwords with password_hash() using PASSWORD_DEFAULT or Argon2id, check them with password_verify(), and rehash older hashes at the next login.
-
Two-factor sign-in for admin accounts
Require a second factor for every admin account, prefer phishing-resistant methods, and keep recovery from becoming a way around MFA.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea