Set httpOnly and secure on PHPSESSID cookie in PHP

Set httpOnly and secure on PHPSESSID cookie in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Hardening PHP Session Cookies: HttpOnly and Secure Flags

When you start working with sessions in a web application, the security posture of the session cookie is as critical as the logic protecting your data store. By default, PHP sets the PHPSESSID cookie without any restrictive flags. While this might seem harmless on a local development machine, it leaves your application vulnerable to Cross-Site Scripting (XSS) attacks and potential interception over unencrypted networks.

The recommended approach is to configure these flags at runtime within your application logic rather than relying solely on .htaccess or server-level directives. This ensures that even if a specific environment or deployment script fails to propagate global settings, the session handler itself enforces the constraints immediately upon creation.

Implementing HttpOnly and Secure in PHP

The HttpOnly flag prevents client-side scripts (like JavaScript) from accessing the cookie via document.cookie. This is your primary defense against XSS attacks. If an attacker manages to inject malicious JavaScript into your page, they cannot steal the session ID because the browser won't expose it to that script.

The Secure flag ensures the cookie is only sent over HTTPS connections. Without this flag, a session ID transmitted over an insecure HTTP connection can be intercepted by network eavesdroppers (Man-in-the-Middle attacks).

Here is how you enforce these settings programmatically before starting a session:

<?php
// Enforce HttpOnly and Secure flags for PHPSESSID
ini_set('session.cookie_httponly', 1);
ini_set('session.cookie_secure', 1);

// Optional but recommended for modern environments
// This prevents the cookie from being sent over non-secure connections
// even if the server doesn't strictly enforce HTTPS at the HTTP layer.
ini_set('session.use_strict_mode', 1);

session_start();

Understanding session.cookie_secure Context

The behavior of the Secure flag is heavily dependent on your server configuration. If you are running behind a load balancer or a reverse proxy (like Nginx or Apache) that handles SSL termination, the incoming request to your PHP backend will often arrive over plain HTTP. In this scenario, setting session.cookie_secure to 1 can break functionality because the session cookie is never sent on the initial request, leading to authentication failures for users.

To resolve this without disabling security entirely, you should check if the current connection is secure before applying the flag:

if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
    ini_set('session.cookie_secure', 1);
} else {
    ini_set('session.cookie_secure', 0);
}

This conditional logic ensures the cookie is only marked as secure when the actual transport layer is encrypted. Note that in many modern hosting environments, $_SERVER['HTTPS'] might be unreliable due to proxy headers. Checking for HTTP_X_FORWARDED_PROTO or using a dedicated library is often more robust.

The Role of session.use_strict_mode

While not explicitly asked in the original query, enabling strict mode is a best practice that complements HttpOnly and Secure. When set to 1, PHP will:

  1. Force the use of the Secure flag automatically if the connection is not HTTPS.
  2. Automatically set the SameSite=Lax attribute, which mitigates certain CSRF risks.

Disabling this (session.use_strict_mode = 0) effectively reverts to legacy behavior where you must manually manage all these attributes. For new projects, leaving it enabled provides a stronger default security posture with minimal code changes.

Framework-Specific Considerations

If you are using a framework like Laravel, the underlying PHP session configuration is often handled automatically once HTTPS is enforced globally. However, understanding the raw flags remains essential for debugging session issues or when configuring custom session drivers.

For instance, if you encounter issues where cookies are not persisting correctly after an SSL migration, verifying these ini_set calls in your bootstrap process (e.g., bootstrap/app.php) is the first line of troubleshooting.

The definitive documentation for these settings can be found in the PHP manual under session.configuration. While the official docs provide the baseline, implementing them dynamically offers granular control that static configuration files sometimes lack. For advanced session management strategies or integrating with specific enterprise security requirements, resources like https://laravelcompany.com offer detailed guides on securing modern PHP stacks.

Our network