How Do You Set Cookie Consent in Laravel? 🍪
December 17, 2024

How Do You Set Cookie Consent in Laravel? 🍪

In this tutorial, we’ll walk you through how to set up the cookie consent field and get it working in your Laravel application. But before we dive in, let’s take a moment to understand what cookies are all about!


What are cookies?

Cookies are important to the functioning of the website and improve your browsing experience! They are small files that websites store on your browser in order to improve your browsing experience. They can save information such as user preferences, site settings, and session details.

Additionally, cookies remain on your browser until they expire or are manually cleared. When you revisit a website, your browser sends the stored cookie back to the server, allowing the website to load your previous preferences or settings, improving usability and personalization.


Why do we need cookie consent?

privacy laws, General Data Protection Regulation (GDPR) in the EU and California Consumer Privacy Act (CCPA) Therefore, in the United States, websites are required to inform users of their data collection practices.

Consent ensures that the website complies with these laws, thereby avoiding penalties and ensuring transparency in data processing.

Cookies may collect personal data such as browsing habits, preferences, and even location. Consent ensures that users know what data is being collected and how it will be used.

By asking for consent, this website enables users to determine whether they are satisfied with the data collected. It complies with the principle of user autonomy over personal information.

Providing correct information about cookies and obtaining consent builds trust between users and websites. Users may interact with websites that respect their privacy.

While some cookies (such as essential functionality cookies) may not require consent, other cookies related to advertising, tracking or analytics usually do. Obtaining consent can help distinguish between these situations while keeping the site user-friendly and compliant.


What is the spatie/laravel-cookie-consent package?

Make your Laravel apps comply with crazy EU cookie laws




This package adds a simple, customizable cookie consent message to your website. When the site loads, a banner appears asking the user to agree to cookies. Once consent is granted, the banner hides and remains hidden.

What this package doesn’t have:

  • Includes an option to “reject” all cookies, which may be necessary.
  • Block trackers and cookies before consenting. You need to deal with this yourself.
  • Include options for different consent categories, such as “Necessary” or “Marketing.”

For more advanced cookie consent options in Laravel, consider these alternatives.

support us

We invest significant resources in creating First-class open source package. You can support us in the following ways Buy one of our paid products.

We would really appreciate you sending us a postcard from your hometown and noting which of our packages you are using. You can find our address at Our contact page

this spatie/laravel-cookie-consent package is a Laravel tool that adds customizable cookie consent banners to your website to help you comply with EU cookie laws. When users visit your site, a banner will appear allowing them to agree to the use of cookies. Once consent is granted, the banner is hidden and remains hidden on subsequent visits.

Main features:

  • Customizable consent message: Easily modify the text and appearance of your consent banner to match your website’s design and messaging.
  • Automatic processing: The package simplifies compliance by managing the display and hiding of consent banners based on user interaction.

limit:

  • There is no “Decline” option: This package does not include a “Decline” button, which may be required for full compliance in some jurisdictions.
  • Do not block cookies before consent: It does not automatically block cookies or trackers without user consent; you need to implement this feature yourself.
  • Disagree Category: The package does not provide options for different consent categories, such as “necessary” or “marketing” cookies.

If you want more advanced cookie consent functionality, alternatives are also available.

  • whitecube/laravel-cookie-consent
  • statikbe/laravel-cookie-consent

In this tutorial we will utilize spatie/laravel-cookie-consent pack.


Install

You can easily integrate this package into your existing project or create a new Laravel application using the following commands.

  • Create a Laravel project.

    composer create-project --prefer-dist laravel/laravel fresh-laravel
    
    
  • Now, let’s continue with the project.

    cd fresh-laravel
    
    
  • we need to install spatie/laravel-cookie-consent pack. The package will be automatically registered during installation.

    composer require spatie/laravel-cookie-consent
    
    
  • You can choose to publish a profile.

    php artisan vendor:publish --provider="Spatie\\CookieConsent\\CookieConsentServiceProvider" --tag="cookie-consent-config"
    
    
  • The content of the published profile is as follows:

    return [
    
        /*
         * Use this setting to enable the cookie consent dialog.
         */
        'enabled' => env('COOKIE_CONSENT_ENABLED', true),
    
        /*
         * The name of the cookie in which we store if the user
         * has agreed to accept the conditions.
         */
        'cookie_name' => 'laravel_cookie_consent',
    
        /*
         * Set the cookie duration in days.  Default is 365 * 20.
         */
        'cookie_lifetime' => 365 * 20,
    ];
    
    
  • The cookie domain is defined by the ‘domain’ key config/session.php document. Make sure you add a new value SESSION_DOMAIN in your .env file. If you use a URL with a port, for example 'localhost:3000'the package will not function properly unless you make this adjustment.

To ensure that the cookie consent field is loaded on every page, please include @include('cookie-consent::index') In the application’s entry point or layout file.


usage

To enable the cookie consent field, add a variable COOKIE_CONSENT_ENABLED to the .env file and set it to true. When enabled, a nice banner appears like this:

When the user clicks “Allow cookies“Button allows cookies, laravel_cookie_consent The variable is set to 1 In your browser’s cookie storage. This value can be used to execute or include any script in the application, such as the Google Analytics code.

@if (!config('cookie-consent.enabled') || \\Illuminate\\Support\\Facades\\Cookie::get(config('cookie-consent.cookie_name')) !== null )
    // conditionally render code here
@endif

Enter full screen mode

Exit full screen mode

All you need to do is establish basic cookie consent in your Laravel application.

Developing advanced cookie management Dashboard Consideration needs to be given to a user-friendly interface that allows users to turn on or off the cookie consent bar without changing the code.

We should also implement a feature to dynamically add scripts in the Laravel application, as well as an option for the user to choose Opt in or opt out Cookie consent per specific script.

No need to worry Jet Ship Laravel SaaS Boilerplate These basic features and more are already included!

In addition to the cookie consent feature, Jetship Laravel SaaS Starter Kit Offers an impressive array of features, including:

  • one Strong and secure authentication system to protect user accounts.
  • Integrate with two leading payment gateways: stripe and lemon juice.
  • A fully managed, SEO Friendly Blog for content marketing.
  • Comprehensive Roles and permissions Fine-grained access control management.
  • Reusable, domain-specific UI elements Accelerate development.
  • and many other powerful tools to simplify your SaaS development.

You can explore the live demo here. Experience the difference the Jetship Laravel Starter Kit makes!


in conclusion:

Integrating the cookie consent banner into your Laravel application is an important step in ensuring compliance with privacy regulations such as GDPR and CCPA. Not only does it enhance user trust, it also increases transparency of data collection and use practices.

by utilizing space/laravel-cookie-agree package, you can implement a fully customizable and user-friendly cookie consent solution with minimal effort. This software package simplifies the process of informing users about cookies, managing their preferences and complying with legal requirements.

Additionally, advanced tools are available, such as Jetship Laravel SaaS Boilerplateyou can take your app’s functionality to the next level – simplifying development while ensuring compliance with built-in cookie consent and other essential features.

With the right tools and a clear implementation strategy, you’re well on your way to creating a more transparent, user-centered experience. Start building apps that not only meet regulatory standards but also enhance user trust and engagement.

Happy coding!

2024-12-17 13:38:44

Leave a Reply

Your email address will not be published. Required fields are marked *