1

I've working on project where lots information stored in Cookies. We've using PHP Laravel 5.2 framework and jQuery library 1.9. Every thing working fine but when trying to read a cookies with jQuery that created with Laravel PHP code its return nothing but when checked it in browser cookies and read with PHP Laravel its working fine

Laravel Code:

Cookie::queue('COOKIE_NAME', $value, $minutes); //Create a cookie

jQuery Code:

var cookieValue = $.cookie("COOKIE_NAME"); //Return nothing
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Govind Samrow
  • 9,981
  • 13
  • 53
  • 90

2 Answers2

1

After few minutes of tested, I found this Is it possible to read HTTPONLY cookies with jQuery?. It means you cannot read HttpOnly cookies, so you should compact cookies value to the view instead or made it available like so, but you will deal with security issues I guess.

Cookie::queue('COOKIE_NAME', 'MyValue', 60, null, null, false, false);

With this kind of setting, you can read your cookies with jquery cookies.

P/S: Cookies value will be encrypted with base64, so you need to decrypt it before using.

Hope this help

Viet Nguyen
  • 2,285
  • 2
  • 26
  • 43
0

You can either make ajax request to retrieve the cookies from your controller or you can create a meta tag as

<meta name="cookie" content="{{ cookie_function() }}" />

in your blade.php file. Then using jquery

var cookieValue = $('meta[name="cookie"]').attr('content');
Rahul
  • 2,374
  • 2
  • 9
  • 17