Cookie Security Flags: HttpOnly, Secure, SameSite
Set cookies like a professional and reduce risks like XSS cookie theft and CSRF.
Andrew Stone
August 5, 2025
9.3k230
Cookies can be made safer using flags.
Key flags
- HttpOnly: JS cannot read cookie (helps reduce damage from XSS)
- Secure: cookie only sent over HTTPS
- SameSite: reduces CSRF by controlling cross-site sends
Example setcookie with options
setcookie("session_id", $value, [
"expires" => time() + 3600,
"path" => "/",
"secure" => true,
"httponly" => true,
"samesite" => "Lax"
]);
Practical note
- Use Secure only when HTTPS is enabled
- SameSite=None requires Secure=true
Next: Email sending, SMTP, and safe mail flows in PHP.
#PHP#Security#Advanced