0

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.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
Furya
  • 413
  • 5
  • 23
  • 1
    You are adding data to cookie from front end and don't push it to server. You should make a ajax call and ask server to add cookie instead. – train_fox Jun 29 '18 at 14:16
  • So why can I access to the cookie from the beginning that I added with this kind of function – Furya Jun 29 '18 at 14:20
  • Are they the same origin & path? Show the cookie in the dev console and the link you are using. – Devon Bessemer Jun 29 '18 at 14:35
  • Laravel encrypts its cookies so if you create one in javascript laravel wont recognize it. Similar question: https://stackoverflow.com/questions/14975751/laravel-4-reading-cookies-set-by-javascript – Marcus Jun 29 '18 at 14:54
  • @MarcusNordin it works !! Thank a lot ! – Furya Jun 29 '18 at 14:56

1 Answers1

1

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

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95