How to Show Cookie Policy Consent or GDPR Popup in Laravel using Cookie.
Cookie in Laravel
You can use the cookie in Laravel using the Helper method `cookie()` or method available with Request/Response Object or `Cookie` facade.
$request->cookie('name');
OR
use Illuminate\Support\Facades\Cookie;
Cookie::get('name');
To get more clarity on how to use the cookie in Laravel with an example to show cookie consent popup on your Laravel website and hide/remove it once you accept the policy.
use Illuminate\Support\Facades\Cookie;
Cookie::get('name');
Cookie Policy Consent Popup in Laravel
First, create and a Cookie Consent popup as notification to the footer section of your Laravel main blade template file.
<div class="cookies-block">
<h5>This website uses cookies</h5>
<p>We use cookies to improve your experience and deliver personalised content. By using this website, you agree to our <a href="javascript:void()">Cookie Policy</a>.</p>
<button class="okay-btn">ACCEPT</button>
</div>
First, Now, Javascript snippet to capture click on the `ACCEPT` button, send ajax request to the server to create a cookie.
$(document).on('click','#footer .cookies-block button', function(e) {
e.preventDefault();
$.ajax({
method: 'GET',
url: '/cookie',
data : {
'name' : 'accept-cookie',
'value' : true
},
success: function (response) {
$('#footer').find('.cookies-block').hide();
},
error: function (error) {
alert('Error: Please refresh the page');
},
});
});
Now you have to create a POST route with the public permission that maps to a method in the controller which returns the response with the cookie.
//in route/web.php
Route::post('/cookie', 'PagesController@setCookie');
//in app/Http/Controllers/PagesController.php
public function setCookie(Request $request)
{
return response('success')->cookie('accept-cookie', true, config('constants.COOKIE_ACCEPT_LIFETIME'));
}
Once Ajax request got success response, it creates a cookie, like as above with name `accept-cookie`.
you can check it in your browser's debug panel (press F12).
As now cookie stored in your browser now you make a check for the user who has already accepted the policy i.e. accept-cookie is stored or not. hence the consent popup can be wrap in check:
//in the main layout template
@if (!request()->cookie('accept-cookie')) //check if cookie not stored
<div class="cookeies-block">
<h5>This website uses cookies</h5>
<p>We use cookies to improve your experience and deliver personalised content. By using this website, you agree to our <a href="javascript:void()">Cookie Policy</a>.</p>
<button class="okay-btn">OK</button>
</div>
@endif
Hope you got an idea. If still having any query or feedback for improvement, feel free to ask.
First, create and a Cookie Consent popup as notification to the footer section of your Laravel main blade template file.
<div class="cookies-block">
<h5>This website uses cookies</h5>
<p>We use cookies to improve your experience and deliver personalised content. By using this website, you agree to our <a href="javascript:void()">Cookie Policy</a>.</p>
<button class="okay-btn">ACCEPT</button>
</div>
$(document).on('click','#footer .cookies-block button', function(e) {
e.preventDefault();
$.ajax({
method: 'GET',
url: '/cookie',
data : {
'name' : 'accept-cookie',
'value' : true
},
success: function (response) {
$('#footer').find('.cookies-block').hide();
},
error: function (error) {
alert('Error: Please refresh the page');
},
});
});
Now you have to create a POST route with the public permission that maps to a method in the controller which returns the response with the cookie.
//in route/web.php
Route::post('/cookie', 'PagesController@setCookie');
public function setCookie(Request $request)
{
return response('success')->cookie('accept-cookie', true, config('constants.COOKIE_ACCEPT_LIFETIME'));
}
Once Ajax request got success response, it creates a cookie, like as above with name `accept-cookie`.
you can check it in your browser's debug panel (press F12).
As now cookie stored in your browser now you make a check for the user who has already accepted the policy i.e. accept-cookie is stored or not. hence the consent popup can be wrap in check:
//in the main layout template
@if (!request()->cookie('accept-cookie')) //check if cookie not stored
<div class="cookeies-block">
<h5>This website uses cookies</h5>
<p>We use cookies to improve your experience and deliver personalised content. By using this website, you agree to our <a href="javascript:void()">Cookie Policy</a>.</p>
<button class="okay-btn">OK</button>
</div>
@endif
Hope you got an idea. If still having any query or feedback for improvement, feel free to ask.
If you need some more options you can use our Package for it. https://github.com/42coders/eu-cookie-consent
ReplyDeletemost help full tutorial: https://devnote.in/how-to-add-gdpr-cookie-consent-in-laravel/
Delete