Session Cookie HTTP and Secure Flags: Implementation on Linux
A security audit flagged your session cookie for lacking the HttpOnly and Secure flags. This is a common finding, but fixing it requires understanding exactly where these directives apply and how they interact with your server environment. If you are running PHP on Apache or Nginx without direct access to the .ini files (like php.ini), you cannot rely on global configuration changes alone. You must either configure the web server layer or implement the logic directly within your application code.
The Role of Server Configuration
If you have control over the web server configuration, this is often the most robust solution because it applies automatically to all users and sessions without requiring code changes.
On an Apache server with mod_php enabled, you can force these flags using .htaccess. Since you mentioned lacking access to the .ini file, this might be your primary option if your hosting provider or system administrator allows overrides. Add the following directives to your .htaccess file:
<IfModule mod_php7.c>
php_flag session.cookie_httponly 1
php_flag session.cookie_secure 1
</IfModule>
However, this approach has limitations. The Secure flag is only effective if the server is actually serving content over HTTPS. If your site is served via HTTP, setting the Secure flag will cause browsers to block the cookie entirely, potentially breaking authentication flows immediately. Therefore, relying solely on .htaccess requires that you have already migrated the application to SSL/TLS.
Implementing in PHP Code
When server-level access is restricted or when you need session-specific control (e.g., different flags for admin vs. guest sessions), you must set these values directly in your PHP code. This is done after the session_start() function but before any output is sent to the browser.
You can modify the cookie properties using the httponly and secure options in the third parameter of session_set_cookie_params():
session_start();
// Set parameters: lifetime, path, domain, secure, httponly
session_set_cookie_params([
'lifetime' => 0, // Session lasts until browser closes
'path' => '/', // Path where the cookie is valid
'domain' => $_SERVER['HTTP_HOST'], // Match current host
'secure' => true, // Send only over HTTPS
'httponly' => true, // Prevent JavaScript access
]);
// Optionally set a specific name if defaults aren't sufficient
session_name('my_app_session');
Alternatively, you can use ini_set() to override the defaults right before starting the session. This is useful if you want to ensure the flags are set even if the global configuration is missing:
ini_set('session.cookie_httponly', 1);
ini_set('session.cookie_secure', 1);
session_start();
Understanding the Flags and Trade-offs
The HttpOnly flag prevents client-side scripts, specifically JavaScript running in the browser, from accessing the session cookie via document.cookie. This is a critical defense against Cross-Site Scripting (XSS) attacks. If an attacker injects malicious script into your application, they cannot steal the session token if this flag is set. Without it, XSS becomes a vector for session hijacking.
The Secure flag instructs browsers to only send the cookie over secure connections (HTTPS). If you set this flag while your site is served over plain HTTP, the browser will ignore the cookie request. This is not an error; it is a security feature designed to prevent credentials from being sent in cleartext on insecure networks.
Handling Mixed Environments
A common pitfall occurs during the migration from HTTP to HTTPS or when using subdomains. If your main domain is example.com (HTTP) but your session storage logic assumes HTTPS, setting the Secure flag prematurely will break login functionality.
If you are in a mixed environment where some pages load over HTTP and others over HTTPS, you might conditionally set the Secure flag based on the scheme:
$secure = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'
|| (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https');
ini_set('session.cookie_secure', $secure);
session_start();
Note that relying on $_SERVER['HTTPS'] requires your proxy or load balancer to forward the protocol information correctly. If behind a reverse proxy, you may need to configure X-Forwarded-Proto headers explicitly in your web server config.
Final Recommendation
For most modern PHP applications, especially those deployed on Linux servers where .ini access is restricted, embedding these settings in the application entry point (e.g., index.php or a framework bootstrap file) is the standard practice. It ensures that every request starts with a secure session context regardless of server configuration quirks.
If you are using Laravel, the session() facade provides methods to configure these attributes per request, but ensuring they are set globally in your service provider is cleaner:
// In AppServiceProvider or a dedicated Session Service Provider
app()->singleton('session.cookie.httponly', function () {
return config('session.cookie.httponly');
});
By combining server-layer enforcement (when possible) with code-level guarantees, you ensure that your application remains resilient against both XSS and man-in-the-middle attacks.