16

If I set a cookie with javascript, how would I read it using Laravel 4?

The reason I'm asking is that the docs say:

All cookies created by the Laravel framework are encrypted and signed 
with an authentication code, meaning they will be considered invalid 
if they have been changed by the client.
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
duality_
  • 17,738
  • 23
  • 77
  • 95
  • https://gist.github.com/DragonI/4450164 is interesting. For now I'm relying on https://stackoverflow.com/a/14979105/470749 – Ryan Mar 29 '18 at 18:47
  • Apparently the cleaner way to handle it in Laravel 5.6: https://stackoverflow.com/a/49578508/470749 – Ryan Mar 30 '18 at 17:11

2 Answers2

25

Just use the native PHP command to retrieve cookies: $_COOKIE['cookie'])

Or perhaps you can set the cookie via an AJAX command (rather than JS doing it itself) - and have Laravel set the cookie supplied by JS on its behalf?

This link confirms setting cookies via AJAX - it will just be a variation of that.

Community
  • 1
  • 1
Laurence
  • 58,936
  • 21
  • 171
  • 212
  • 1
    Any idea how I could do the reverse? Set a cookie in Laravel that javascript can read? I tried `$_COOKIE['cookie']` with no luck – A F Dec 04 '14 at 16:18
20

In Laravel 5.6 (and maybe earlier versions too):

Specify the cookie name in the $except array within App\Http\Middleware\EncryptCookies.php.

It tells Laravel that those cookies aren't encrypted (and therefore don't need to be decrypted when read).

(P.S. Thanks to https://github.com/laravel/laravel/pull/460#issuecomment-377537771)

Ryan
  • 22,332
  • 31
  • 176
  • 357