Laravel 4:读取由 javascript 设置的 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14975751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:34:48  来源:igfitidea点击:

Laravel 4: reading cookies set by javascript

laravellaravel-4

提问by duality_

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

如果我使用 javascript 设置 cookie,我将如何使用 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.

回答by Laurence

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

只需使用本机 PHP 命令来检索 cookie: $_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?

或者你可以通过 AJAX 命令设置 cookie(而不是 JS 自己做) - 并让 Laravel 代表它设置由 JS 提供的 cookie?

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

此链接确认通过 AJAX 设置 cookie- 这只是它的一个变体。

回答by Ryan

In Laravel 5.6 (and maybe earlier versions too):

在 Laravel 5.6(也可能是更早的版本)中:

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

在 内的$except数组中指定 cookie 名称App\Http\Middleware\EncryptCookies.php

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

它告诉 Laravel 这些 cookie 没有加密(因此在读取时不需要解密)。

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

(PS 感谢https://github.com/laravel/laravel/pull/460#issuecomment-377537771