Furya
Furya

Reputation: 463

Laravel can't access all cookies

I'm using laravel 5.6 and cookie to use data across pages.

In my first page N°1, I have a table with button like this :

<button data-href="/fractions/" data-lot="1" class="link m-portlet__nav-link btn m-btn m-btn--hover-brand m-btn--icon m-btn--icon-only m-btn--pill" title="Fractions">
    <i class="la la-puzzle-piece"></i>
</button>

And I have a script attachi to .link :

$(document).on('click', '.link', function(e){
    $.cookie("lot", $(this).data("lot"), { expires: 7 });
    $(location).attr('href',$(this).data("href"));
}); 

On my second page N°2 (data-href), I use :

{{ Cookie::get('lot') }}

But nothing shows up. I looked inside chrome console and I can see my cookie.

I try to debug on the controller side but it doesn't see the cookie too.

What's weird is that I use the same system at the beginning of my site (page N°0) and on page N°2 I can access to the cookie I create on page N°0.

So why I can access all cookies?

Thank for your help.

Upvotes: 0

Views: 1420

Answers (1)

Devon Bessemer
Devon Bessemer

Reputation: 35367

path: '/'

Define the path where the cookie is valid. By default the path of the cookie is the path of the page where the cookie was created (standard browser behavior). If you want to make it available for instance across the entire domain use path: '/'. Default: path of page where the cookie was created.

Create expiring cookie, valid across entire site:

$.cookie('name', 'value', { expires: 7, path: '/' });

Source: https://github.com/carhartl/jquery-cookie#readme


For Laravel, since it uses Cookie Encryption, consult Laravel 4: reading cookies set by javascript

Upvotes: 1

Related Questions