How to Fix Cookies Missing the Secure Flag on HTTPS

Your page sets one or more cookies without the `Secure` attribute, so a browser may send them over unencrypted HTTP. In this guide you will find every offending Set-Cookie header, add `Secure` at the source (app, framework or web server), and confirm that every cookie your page returns is HTTPS-only.

By Updated ⏱ 20 minutes🎯 Beginner● Medium impactSecurity
Illustration: Cookies use the Secure flag fix guide
SECURITY · FIX GUIDE Cookies use the Secure flag IMPACTEFFORT TIME20 minutes LEVELBeginner

Why it matters

A cookie without Secure is attached to any plain-HTTP request to your domain. That includes a mistyped http:// link, a hotel Wi-Fi captive portal, or a downgrade attack. An attacker on the same network can read session or login cookies from that traffic and take over accounts.

Browsers, PCI DSS scanners and security reviewers all treat a missing Secure flag as a basic hygiene failure. It costs nothing to fix on a site that already runs on HTTPS, and it does not affect rankings or page speed.

✕BeforeSet-Cookie: sessionid=a1b2c3; Path=/;HttpOnly✓AfterSet-Cookie: sessionid=a1b2c3; Path=/;Secure; HttpOnly; SameSite=Lax

What you need

The site served fully over HTTPS with a valid certificate
Access to application code, CMS config files or the web server/proxy config
A terminal with curl, or Chrome/Firefox DevTools
A staging environment or a test login to confirm sessions still work

Step-by-step fix

1

List every cookie the page sets

The audit reads the Set-Cookie headers in your page's HTTP response and flags any cookie that lacks Secure. Start by requesting the page and printing those headers. Also check pages that set extra cookies, such as login, cart and language pages.

com. Any cookie without a tick in the Secure column needs fixing. Note each cookie's name so you can trace which code or plugin sets it.

text
curl -sI https://www.example.com/ | grep -i '^set-cookie'
curl -sI https://www.example.com/login | grep -i '^set-cookie'
Pro tip: Cookie names are good clues to their source: PHPSESSID is a PHP session, connect.sid is express-session, laravel_session is Laravel, and wp_* is WordPress.
2

Make sure the app knows it is on HTTPS

Many frameworks add Secure automatically, but only when they detect HTTPS. Behind a load balancer, CDN or reverse proxy that terminates TLS, the app often sees plain HTTP and leaves the flag off. Forward the protocol header from the proxy and tell the app to trust it.

Also redirect all HTTP traffic to HTTPS, because browsers refuse to store Secure cookies set over http://.

nginx
# Nginx in front of the app
proxy_set_header X-Forwarded-Proto $scheme;

// Express behind a proxy
app.set('trust proxy', 1);
Pro tip: In Express, `req.secure` returning false on an HTTPS URL is a sign that the trust proxy setting is missing.
3

Add Secure where your code sets cookies

Find each place your code writes a cookie and turn on the secure option. Set HttpOnly and SameSite at the same time, unless JavaScript genuinely needs to read the cookie. In PHP, use the options-array form of setcookie().

In Node, pass secure: true.

javascript
// Express
res.cookie('sessionid', value, { secure: true, httpOnly: true, sameSite: 'lax', path: '/' });

// PHP 7.3+
setcookie('prefs', $value, [
  'expires' => time() + 86400 * 30,
  'path' => '/',
  'secure' => true,
  'httponly' => true,
  'samesite' => 'Lax',
]);
Pro tip: Cookies written with document.cookie in the browser need '; Secure' added to the string too, for example: document.cookie = 'theme=dark; Path=/; Secure; SameSite=Lax'.
4

Turn on secure session cookies in your framework

Session and CSRF cookies are usually created by the framework, not your own code, so you set the flag in configuration instead. Common settings:

  • PHP: session.cookie_secure = 1 in php.ini or via ini_set
  • express-session: cookie: { secure: true }
  • Django: SESSION_COOKIE_SECURE = True and CSRF_COOKIE_SECURE = True
  • Laravel: SESSION_SECURE_COOKIE=true in .env
  • Rails: config.force_ssl = true
text
; php.ini
session.cookie_secure = 1
session.cookie_httponly = 1
session.cookie_samesite = "Lax"
Pro tip: After changing php.ini, restart PHP-FPM (for example: sudo systemctl restart php8.3-fpm) or the change will not apply.
5

Add a server-level safety net for remaining cookies

Third-party plugins or legacy code sometimes keep setting insecure cookies. The web server or proxy can add Secure to every Set-Cookie header on the way out. 3+, use proxy_cookie_flags.

On Apache with mod_headers, use a regex edit that skips cookies already flagged, so you do not end up with duplicates.

nginx
# Nginx (inside the location that proxies to the app)
proxy_cookie_flags ~ secure samesite=lax;

# Apache (.htaccess or vhost, requires mod_headers)
Header always edit Set-Cookie "^((?!.*;\s*[Ss]ecure).*)$" "$1; Secure"
Pro tip: Treat this as a backstop. Fixing cookies at the source keeps them correct if you later move hosts or proxies.
6

Re-test headers and confirm logins still work

com, or use a private window. Request the pages from step 1 again: every Set-Cookie line should now contain Secure. Then log in, add an item to the cart and switch language, to confirm sessions survive.

Finally, re-run the audit.

text
curl -sI https://www.example.com/ | grep -i '^set-cookie' | grep -vi 'secure' || echo 'All cookies have Secure'
Pro tip: If a cookie disappears after the change, the page that set it was probably still served over HTTP. Fix the redirect rather than removing the flag.

Platform-specific notes

WordPress

WordPress core sets its auth cookies as Secure automatically when is_ssl() is true. Make sure Settings > General shows https:// for both URLs. Behind a proxy or CDN, add `if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { $_SERVER['HTTPS'] = 'on'; }` to wp-config.php above the 'stop editing' line. For plugins that use PHP sessions, add @ini_set('session.cookie_secure', 1); to wp-config.php. Plugins that call setcookie() directly need an update, a support ticket or the server-level rule from step 5.

Shopify

Shopify's own cookies (cart, _shopify_y, session cookies) are served over HTTPS and you cannot edit their headers. A flagged cookie usually comes from a third-party app or a theme script using document.cookie. Check Online Store > Themes > Edit code (theme.liquid and any assets/*.js) and add '; Secure' to those cookie strings. For app-set cookies, contact the app developer.

Next.js

In the App Router, set the option in Route Handlers, Server Actions or middleware: cookies().set('sessionid', value, { secure: true, httpOnly: true, sameSite: 'lax', path: '/' }). In Next.js 15+, cookies() is async, so await it first. If you use NextAuth/Auth.js, it uses __Secure- prefixed cookies when AUTH_URL/NEXTAUTH_URL starts with https://, so make sure that variable is set correctly in production.

Cloudflare

Cloudflare's own cookies, such as __cf_bm, are already Secure. Response Header Transform Rules cannot reliably rewrite individual Set-Cookie values. To patch cookies from an origin you cannot change, use a Worker that reads response.headers.getSetCookie() and appends '; Secure' to any cookie missing it. Turn on SSL/TLS > Edge Certificates > Always Use HTTPS as well.

Nginx / Apache

proxy_cookie_flags only applies to responses proxied with proxy_pass. For PHP-FPM via fastcgi_pass, set session.cookie_secure in PHP instead. On Apache, enable mod_headers first (a2enmod headers) and reload. The 'always' keyword makes the edit apply to error and redirect responses too.

Check that it worked

✅ How to verify

  • Run curl -sI https://www.example.com/ | grep -i set-cookie and confirm every line contains Secure.
  • In Chrome DevTools > Network, select the document request and check that each Set-Cookie response header lists Secure. Under Application > Cookies, the Secure column should be ticked for every cookie.
  • Log in, log out and use the cart or forms in a fresh private window to confirm sessions still persist.
  • Re-run the audit and confirm the 'Cookies use the Secure flag' check passes.

⚠️ Common mistakes

  • Hard-coding secure: false for local development and shipping it to production. Use an environment check such as secure: process.env.NODE_ENV === 'production'.
  • Terminating TLS at a proxy without forwarding X-Forwarded-Proto, so the framework believes it is on HTTP and silently drops the Secure flag.
  • Using a blanket Apache rule like Header edit Set-Cookie ^(.*)$ "$1; Secure", which duplicates the attribute on cookies that already have it.
  • Fixing only the homepage while login, checkout or language-switch responses still set insecure cookies.

Is this still an issue on your site? Run a free audit — ZORO checks this and 294 other SEO, AI-visibility, accessibility and security signals, with fixes for your platform.

Check my site free

FAQ

What does the Secure flag on a cookie do?

It tells the browser to send the cookie only over HTTPS connections. The cookie is never attached to plain http:// requests, where anyone on the network could read it.

Do I need the Secure flag if my whole site is HTTPS?

Yes. Even with full HTTPS, a single http:// request can expose a cookie that lacks Secure. That request might come from an old link, a typed URL before the redirect, or a network attacker forcing a downgrade. HSTS reduces this risk, but Secure closes it for each cookie.

Will adding Secure break my site on localhost?

Chrome and Firefox treat http://localhost as a secure context and accept Secure cookies there. Custom local hostnames over plain HTTP do not. If you test on one of those, enable the flag conditionally by environment or use HTTPS locally with a tool like mkcert.

What is the difference between Secure, HttpOnly and SameSite?

Secure limits the cookie to HTTPS. HttpOnly hides it from JavaScript, which helps against XSS theft. SameSite controls whether it is sent on cross-site requests, which helps against CSRF. They protect against different attacks, so session cookies should normally have all three.