我如何在 Laravel 中获取 cookie 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43996363/
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
How can i get cookie value in laravel
提问by shakir
I am setting cookie in in blade file using javascript by below piece of code
我正在使用 javascript 通过以下代码在刀片文件中设置 cookie
function showPosition(position) {
var lat=position.coords.latitude;
var lng=position.coords.longitude;
document.cookie="lat=" + lat;
document.cookie="lng=" + lng;
//var allcookies = document.cookie;
//alert(allcookies);
localStorage.setItem('latLng',latLng);
}
and in model i try to get cookie
在模型中,我尝试获取 cookie
$lat = Cookie::get('lat');
$lng = Cookie::get('lng');
dd($lat);
but it shows me null. Any help please.
但它显示我为空。请任何帮助。
回答by Gábor Nádai
Laravel stores cookies encrypted, so Cookie::get()
will only retrieve cookies set by Laravel. You should use the native $_COOKIE['lat']
in PHP to retrieve cookies set for example in JavaScript.
Laravel 存储加密的 cookie,因此Cookie::get()
只会检索 Laravel 设置的 cookie。您应该使用$_COOKIE['lat']
PHP 中的本机来检索例如在 JavaScript 中设置的 cookie。
You can find out more here:
在这里你可以找到更多:
回答by Mayank Pandeyz
Try to understand the concept first. Check this line:
先试着理解这个概念。检查这一行:
localStorage.setItem('latLng',latLng);
You are using localStorage
here and then:
你在localStorage
这里使用,然后:
$lat = Cookie::get('lat');
you are trying to get it with Cookie
.
你正试图用Cookie
.