douhuitan5863
2017-05-16 08:37
浏览 358
已采纳

如何在laravel中获得cookie值

I am setting cookie in in blade file using javascript by below piece of code

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

$lat = Cookie::get('lat');
$lng = Cookie::get('lng');
dd($lat);

but it shows me null. Any help please.

图片转代码服务由CSDN问答提供 功能建议

我使用javascript通过下面的代码在刀片文件中设置cookie < pre> 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); }

在模型中我尝试获取cookie

  $ lat = Cookie :: get('lat'); 
 $  lng = Cookie :: get('lng'); 
dd($ lat); 
   
 
 

但它显示为null。 任何帮助请。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doujiaoben28596 2017-05-16 08:46
    已采纳

    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.

    You can find out more here:

    https://laravel.com/docs/4.2/requests#cookies

    已采纳该答案
    打赏 评论
  • doucheng3811 2017-05-16 08:40

    Try to understand the concept first. Check this line:

    localStorage.setItem('latLng',latLng);
    

    You are using localStorage here and then:

    $lat = Cookie::get('lat');
    

    you are trying to get it with Cookie.

    打赏 评论

相关推荐 更多相似问题