How to disable cookies in a generic way till cookies are accepted by user

How to disable cookies in a generic way till cookies are accepted by user

July 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to disable cookies in a generic way till cookies are accepted by user

Disabling Cookies Until Acceptance: A Generic Solution

Cookies are an essential part of modern web development, serving as small text files that store information on your computer or mobile device to provide personalized experiences for users across multiple sessions. However, with the implementation of data protection regulations like GDPR, it has become crucial to obtain user consent before setting cookies.

The Challenge: Disabling Cookies Without Rewrite

In a scenario where you have an existing webshop that heavily relies on cookies, rewriting the entire system to comply with GDPR standards can be a substantial undertaking. This leads us to seek a generic solution—one that allows us to “disable” cookie functionality until the user explicitly accepts them.

One straightforward approach is to remove all set-cookie headers sent by our server. By doing so, we can effectively disable any new cookies from being set on the client-side. We can achieve this through either nginx configuration or PHP logic depending on your stack.

Disabling Cookies via Nginx Configuration

To unset all set-cookie headers using Nginx, you can utilize the map directive in combination with regular expressions. Here’s a basic example:

map $http_cookie $cookie {
    ~*(\;|$) "Set-Cookie:(.*?);" $2;
}

server {
  # ...
}

In this configuration, Nginx will remove any set-cookie headers present in the HTTP response.

Managing External Cookies and JavaScript

While removing server-side cookies is a starting point, two significant challenges remain:

  1. Preventing external sites from setting cookies: When your page includes content from third-party sources like social media buttons or ads, these services may also set their own cookies. To prevent this, you can use the SameSite attribute for your own cookies, which tells browsers how to handle them in cross-site requests.

  2. Stopping JavaScript from setting cookies: Modern web applications often rely on client-side scripts to set and manage cookies. Modifying every script source to comply with GDPR requirements would be impractical. However, you can override browser functions to prevent cookie setting via JavaScript. This involves creating a custom version of the document.cookie property that disables writing functionality until user acceptance.

A Real-World Example

At LaravelCompany, we’ve developed a solution called the “Cookie Consent Bar” to handle this scenario elegantly. Our plugin intercepts all outgoing requests, removing set-cookie headers until the user actively accepts cookies through an intuitive, GDPR-compliant consent dialogue.

Conclusion: A Balanced Approach

While complete cookie disablement might seem like a straightforward solution, a more practical approach involves temporarily suppressing cookie functionality while presenting users with clear and accessible means to provide their consent. By leveraging server-side configuration adjustments and custom JavaScript overrides, we can achieve GDPR compliance without the need for extensive code refactoring.

Remember, the key is not just disabling cookies but providing a seamless user experience that respects privacy regulations without sacrificing essential functionality. As always, it’s crucial to stay informed about evolving legal requirements in your region and consult with a legal expert if needed.

In our next post at LaravelCompany, we’ll explore the intricacies of implementing a cookie consent dialogue that respects user choice while maintaining compatibility with popular web frameworks. Stay tuned!

Configuration CLI

Our network