Session Cookie Missing Secure Flag: How to Fix
If your scanner says the session cookie is missing the Secure attribute, add Secure to the cookie's Set-Cookie header in your framework's session settings, then confirm it in your browser's DevTools.
This post is for owners and developers of small sites who got that line in a scan or audit report and want to know what it means and how to fix it. You will learn what Secure, HttpOnly and SameSite each do. You will get copy-paste fixes for PHP, WordPress, Node/Express and Spring. You will also get a two-minute check to prove the fix worked.
What the Secure attribute on a session cookie does
A session cookie is a small piece of data your site gives a visitor's browser after login. The browser sends it back on every request, and that is how your site knows who is logged in. Anyone who copies that cookie can often act as that user.
The Secure attribute tells the browser: only send this cookie over HTTPS. Without it, the browser may also send the cookie over plain HTTP, where anyone on the same network (café Wi-Fi, for example) could read it.
That is the meaning behind the scanner messages "session cookie missing secure attribute" and "session cookie does not contain the secure attribute". The cookie works fine. It just isn't restricted to encrypted connections. The MDN Set-Cookie reference describes the behavior in detail.
How serious is this finding?
Usually low to medium. If your whole site is HTTPS and redirects every HTTP request, the risk is smaller. But a stray HTTP request (an old link, a typed address, a mixed-content image) can still leak the cookie when the flag is missing. It is a cheap fix, so do it.
Two honest caveats:
- Check which cookie was flagged. Scanners sometimes flag harmless cookies like a language preference. Only login and session cookies need this treatment first.
- Secure is not a full defense. It protects the cookie in transit. It does not stop cross-site scripting (XSS, where an attacker runs script in your pages) or stolen passwords.
Secure vs HttpOnly vs SameSite
These three attributes are the core of session cookie security best practices. They protect against different problems, so you want all three on a session cookie. The OWASP Session Management Cheat Sheet recommends them.
| Attribute | What it does | Main risk it reduces | Typical value |
|---|---|---|---|
Secure | Browser only sends the cookie over HTTPS | Sniffing on untrusted networks | Present (no value) |
HttpOnly | JavaScript cannot read the cookie | Cookie theft through XSS | Present (no value) |
SameSite | Limits when the cookie is sent on cross-site requests | Cross-site request forgery (CSRF, tricking a logged-in user's browser into an unwanted action) | Lax or Strict |
A good session cookie looks like this in the response header:
Set-Cookie: sid=abc123; Path=/; Secure; HttpOnly; SameSite=Lax
A few notes on SameSite:
Laxis a safe default for most sites. Users stay logged in when they click a link to your site from elsewhere.Strictnever sends the cookie on cross-site navigation. It is stronger, but a user arriving from an email link may look logged out on the first page.Noneallows cross-site use (for example, an embedded widget) and requiresSecure. Only use it if you truly need it.
Before you start: make sure HTTPS works everywhere
Once you add Secure, the browser will refuse to send the cookie over HTTP. If any part of your site still loads over HTTP, logins can break there.
- Visit
http://yourdomain.comand confirm it redirects tohttps://. - Check that subdomains (like
wwwandapp) also serve valid certificates. - Test in a private window after each change, so old cookies don't confuse the result.
Once HTTPS is solid, consider the Strict-Transport-Security header, which tells browsers to always use HTTPS for your domain. Our security headers tool shows whether you have it. Add it only after HTTPS works on every subdomain you include.
Fix it in PHP
PHP's default session cookie is PHPSESSID. You can set the flags globally or in code.
Option 1: php.ini (or your host's PHP settings panel, or .user.ini on many shared hosts):
session.cookie_secure = 1
session.cookie_httponly = 1
session.cookie_samesite = "Lax"
Option 2: in code, before session_start() (PHP 7.3 or newer):
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'Lax',
]);
session_start();
The flags must be set before the session starts. See the PHP session security settings for details.
Fix it in WordPress
WordPress core handles its own login cookies and sets them as Secure when it knows the site is on HTTPS. The most common problem is that WordPress doesn't realize it is on HTTPS, usually because a proxy, CDN or load balancer sits in front. A plugin or theme may also start a plain PHP session (the PHPSESSID cookie), which core doesn't control.
WordPress core login cookies (wordpress_logged_in_* and wordpress_sec_*) are Secure and HttpOnly on HTTPS, but they usually show no SameSite attribute, because WordPress itself does not add one. The ini_set lines below only affect PHPSESSID cookies started by plugins or themes. They do not change the core login cookies.
- Go to Settings > General and make sure both WordPress Address and Site Address start with
https://. - Open
wp-config.phpand add these lines above the comment that says "That's all, stop editing":
// If a proxy or CDN terminates HTTPS, tell WordPress the request was secure.
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
$_SERVER['HTTPS'] = 'on';
}
define('FORCE_SSL_ADMIN', true);
// Covers PHPSESSID cookies started by plugins or themes (not core login cookies).
@ini_set('session.cookie_secure', '1');
@ini_set('session.cookie_httponly', '1');
@ini_set('session.cookie_samesite', 'Lax');
- Clear your site and CDN cache, log out, and log in again.
Only use this snippet if your proxy or CDN always sets or overwrites X-Forwarded-Proto, and the site cannot be reached directly, bypassing the proxy. If the flagged cookie still isn't fixed, find out which plugin sets it (the DevTools check below shows the cookie name) and check its settings or update it.
Fix it in Node.js and Express
For express-session, set the cookie options and tell Express it sits behind a proxy if you use one (Heroku, Render, Nginx, Cloudflare and similar):
const session = require('express-session');
// Only if a reverse proxy handles HTTPS.
// Set the number to how many proxies sit in front of your app
// (for example 2 for Cloudflare plus Nginx).
app.set('trust proxy', 1);
app.use(session({
name: 'sid',
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
secure: process.env.NODE_ENV === 'production',
httpOnly: true,
sameSite: 'lax',
maxAge: 1000 * 60 * 60 * 8 // 8 hours
}
}));
Use 1 for a single proxy. If you have two hops (for example Cloudflare in front of Nginx), the number is different, so check your setup.
The classic gotcha: with secure: true and no trust proxy, Express thinks the connection is HTTP and never sends the cookie, so logins silently fail. See the express-session documentation for how this works.
Fix it in Spring and Spring Boot
In Spring Boot, add these to application.properties (Boot 2.6 or newer supports same-site):
server.servlet.session.cookie.secure=true
server.servlet.session.cookie.http-only=true
server.servlet.session.cookie.same-site=lax
If a proxy terminates HTTPS, also add:
server.forward-headers-strategy=framework
For a traditional WAR deployment, use web.xml:
<session-config>
<cookie-config>
<http-only>true</http-only>
<secure>true</secure>
</cookie-config>
</session-config>
The property names are listed in the Spring Boot application properties reference.
Confirm the fix in your browser
Don't trust the config file. Check the live site.
- Open your site in Chrome or Edge, log in, and press F12.
- Go to the Application tab, then Cookies, then click your domain. (In Firefox it is the Storage tab.)
- Find your session cookie (often
PHPSESSID,sid,JSESSIONIDor awordpress_logged_in_...name). - Check that the Secure and HttpOnly columns have a tick and SameSite shows
LaxorStrict. WordPress core login cookies (wordpress_logged_in_*,wordpress_sec_*) are Secure and HttpOnly on HTTPS but usually show no SameSite value, because WordPress core does not add one. A blank SameSite on those cookies is normal.
To see the raw header, open the Network tab, reload, click the first request, and look for Set-Cookie under Response Headers. You can also check from a terminal:
curl -s -D - -o /dev/null https://yourdomain.com/login | grep -i set-cookie
If no cookie appears, the page may only set it after login. Use DevTools while logged in instead. Then re-run your scanner to make sure the finding is gone.
A stronger option: the __Host- prefix
For extra protection, you can name the cookie with the __Host- prefix (for example __Host-sid). Browsers only accept it if it has Secure, Path=/ and no Domain attribute. This stops other subdomains from overwriting it. Not every framework makes this easy, so treat it as a nice extra, not a must-do.
Quick checklist
- HTTP redirects to HTTPS on every domain and subdomain.
- Session cookie has
Secure,HttpOnlyandSameSite=Lax(orStrict). The exception is WordPress core login cookies, which usually show no SameSite value. - Proxy or CDN settings pass the HTTPS information to your app.
- You checked the real cookie in DevTools, logged in.
- Session cookies expire after a sensible time, and sessions end on logout.
Where an audit fits
This cookie flag is one small item. A free passive check can spot missing flags like this from the outside. A scanner can't tell you whether your login logic, access controls or session handling have deeper problems. If you want to know how the two approaches differ, read manual vs automated penetration testing.
Key takeaways
- The Secure attribute stops the browser from sending your session cookie over plain HTTP. Add it to every session or login cookie.
- Set all three:
Secure,HttpOnlyandSameSite=Lax(orStrict). Each blocks a different attack. - Behind a proxy or CDN, tell your app the request was HTTPS, or the cookie may not be set at all.
- Verify in DevTools (Application > Cookies) while logged in, then re-scan.
- Make sure HTTPS works on every page and subdomain before you flip the switch.
Want a person to look at the rest?
If this finding came from an automated scan, there may be more behind it. A Bug Circuit Circuit audit is $49 one-time. A human security engineer manually reviews your site and writes up each issue with severity, evidence and exact fixes. It is a focused small-site audit, not an enterprise pentest or a compliance certificate. If the cookie fix above is all you needed, you are done, and that is a good result too.
A real human security engineer audits your whole site by hand and sends a full report: every issue, its severity, and the exact fix. From $49, with a 14-day money-back guarantee.
See pricing