how to set secure flag on cookies in laravel

how to set secure flag on cookies in laravel

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

Stefan Bogdanescu

Founder & Senior Architect

Enforcing Secure Cookies Across Your Laravel Application

When you configure cookies in a web application, the secure flag dictates whether that cookie is transmitted only over encrypted connections (HTTPS) or if it can be sent over unencrypted HTTP as well. In modern development environments where most traffic is expected to be secure, ensuring this flag is set correctly isn't just a best practice; it's often a security requirement. If you leave the secure flag unset on cookies served over HTTPS, browsers may still send them over insecure connections if they are ever accessed via HTTP, potentially exposing sensitive data like session tokens to man-in-the-middle attacks.

In Laravel, handling this globally is straightforward because the framework provides centralized configuration points that apply to every controller and view rendered by your application. You do not need to manually append ->secure(true) to every Cookie::make() call throughout your codebase. Instead, you can set a default behavior that persists across the entire application lifecycle.

The primary method to enforce this globally is through environment variable configuration. Laravel checks for specific environment variables before falling back to default values defined in the .env file or constructor options. To ensure all cookies are served securely, you should set the APP_COOKIE_SECURE environment variable to true. This can be done directly in your .env file:

APP_COOKIE_SECURE=true

When this variable is present and set to true, Laravel automatically applies the secure flag to every cookie it creates via the Cookie, Session, or Request facade. This approach is particularly effective for applications deployed on platforms like Heroku or AWS where HTTP traffic might be unavoidable during initial page loads, but you want to ensure that once a connection is established over HTTPS, cookies are never leaked over plain text.

However, there is a significant technical nuance regarding the APP_COOKIE_SECURE variable: it only takes effect if your application is running over HTTPS. If you deploy your Laravel app behind a load balancer or a reverse proxy (like Nginx or Apache) that terminates SSL at the edge, the request reaching your PHP-FPM process might arrive as HTTP even though the user's browser sees HTTPS. In such scenarios, relying solely on APP_COOKIE_SECURE=true can cause issues because Laravel detects an insecure channel and refuses to set cookies with the secure flag, effectively breaking the session for users.

To resolve this mismatch, you must inform Laravel that it is actually running in a secure context despite the underlying connection appearing as HTTP. This is done by setting the APP_ENV variable or, more commonly, by using the APP_URL and ensuring your proxy headers are correctly configured. But specifically for cookie security, if your proxy sends the X-Forwarded-Proto header with a value of https, you should also set the APP_COOKIE_SECURE variable to true only after confirming that your application is consistently accessible via HTTPS.

Alternatively, if you prefer not to rely on environment variables or find yourself needing more granular control within specific environments, you can configure the cookie security behavior directly in your Laravel configuration file. While less common for simple global settings, this involves modifying the config/session.php file or defining a service provider that overrides the default cookie options.

// In app/Http/Middleware/TrustProxies.php (if applicable)
// Or by creating a custom Service Provider
use Illuminate\Support\Facades\Cookie;

public function boot()
{
    // Force all cookies to be secure if needed globally via facade override
    // Note: Global overrides via facades are generally discouraged in favor of env vars.
}

The preferred approach remains the environment variable APP_COOKIE_SECURE=true combined with a properly configured reverse proxy that sends the correct headers. This ensures that when a user visits your site, whether they click a link from an insecure page or navigate directly to HTTPS, the browser receives cookies strictly over encrypted channels. Always verify that your .env file is not committed to version control and that these settings align with your deployment strategy.

Our network