Laravel 4: reading cookies set by javascript

梦想与她 提交于 2019-12-18 19:12:11

问题


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.

回答1:


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.




回答2:


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)



来源:https://stackoverflow.com/questions/14975751/laravel-4-reading-cookies-set-by-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!