php 如何在 Laravel 中设置和获取 Cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45207485/
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-08-26 03:05:33  来源:igfitidea点击:

How to set and get Cookie in laravel

phplaravelcookieslaravel-5.3

提问by S.M_Emamian

I would like to set and get value in cookie but it doesn't work:

我想在 cookie 中设置和获取值,但它不起作用:

    Cookie::queue('online_payment_id', "1", 15);

    $value = Cookie::get('online_payment_id');
    dd($value);

dd()returns null;

dd()返回null



I used below way but I got this message:

我使用以下方式,但我收到了这条消息:

Method cookie does not exist.

    request()->cookie('online_payment_id');

    $value = response()->cookie('online_payment_id', "1", 15);
    dd($value);

采纳答案by Hariharan

Set Cookies

设置 Cookie

 public function setCookie(Request $request){
      $minutes = 60;
      $response = new Response('Set Cookie');
      $response->withCookie(cookie('name', 'MyValue', $minutes));
      return $response;
   }

Get Cookie

获取饼干

   public function getCookie(Request $request){
      $value = $request->cookie('name');
      echo $value;
   }

回答by Prashant Barve

Add at the top of the file add use Symfony\Component\HttpFoundation\Cookie;or simply use Cookie;

在文件顶部添加 adduse Symfony\Component\HttpFoundation\Cookie;或干脆use Cookie;

To Generating Cookie Instances

生成 Cookie 实例

    $cookie = cookie('name', 'value', $minutes);
    return response('Hello World')->cookie($cookie);

Retrieving Cookies From Requests you can use Request be sure you use Request $requestin you method.

从请求中检索 Cookie,您可以使用 Request 确保Request $request在您的方法中使用。

    $value = $request->cookie('name');

回答by Erin Geyer

Even if you carefully follow the Laravel documentation regarding how to set cookies, you may end up going crazy for hours (as I did today) because you just can't get your cookies set!

即使您仔细遵循 Laravel 文档中关于如何设置 cookie 的内容,您也可能会疯狂几个小时(就像我今天所做的那样),因为您无法设置 cookie!

I finally discovered why my cookies weren't being set... I was also using the dump()function to display data while I tested the code. The dump()function sends output to the browser, which requires headers to be sent. Cookies also have to be sent with the headers, so if you're using dump(), your cookies will never be sent!

我终于发现了为什么我的 cookie 没有被设置......我也在dump()测试代码时使用该函数来显示数据。该dump()函数将输出发送到浏览器,这需要发送标头。Cookie 也必须与标头一起发送,因此如果您使用的是dump(),您的 Cookie 将永远不会被发送!

I hope this helps others who will very likely run into this situation.

我希望这可以帮助其他很可能会遇到这种情况的人。

回答by gaurav

Like everything else in laravel there are many ways of set/get cookies. The cookie will automatically be added to the outgoing response.

就像 Laravel 中的其他所有东西一样,有很多设置/获取 cookie 的方法。cookie 将自动添加到传出响应中。

    $value = 1;
    $minutes = 15;
    Cookie::queue($online_payment_id, $value, $minutes);

In order to get the cookie you can use the

为了获得cookie,您可以使用

    request()->cookie($online_payment_id);

回答by Mahedi Hasan Durjoy

public function setCookie(Request $request) {

    Cookie::queue('name', $request->test, 10);

    return view('home');
 }

Here Cookie::queue($cookie_name, $data, $life_time_of_this_cookie);

这里 Cookie::queue($cookie_name, $data, $life_time_of_this_cookie);

and simply print cookie where you want to show data.

并简单地在您想要显示数据的地方打印 cookie。

 {{ Cookie::get('name') }}

回答by laktherock

There are several methods to set and get cookies in laravel.

在 laravel 中有几种设置和获取 cookie 的方法。

Official documentation says Cookies

官方文档说Cookies

I usually ended up this way

我通常是这样结束的

$response = new \Illuminate\Http\Response(view('welcome'));
$response->withCookie(cookie('test_cookie', $request->test_cookie, 45000));
return $response;

You can also use CookieJar

你也可以使用 CookieJar

Refer CookieJar

参考CookieJar

回答by sh6210

to show all cookies

显示所有 cookie

request()->cookie();

to get specific item

获取特定项目

request()->cookie('itm_name');

to set item

设置项目

request()->cookie('item_name', 'item_value', Min_how_long_it_will_alive);

回答by saroj

Try this:

尝试这个:

public function setCookie(Request $request){

 $cookie_name = "user";
 $cookie_value = "value";
 setcookie($cookie_name,$cookie_value, time() + (86400 * 30), "/"); //name,value,time,url

 dd($_COOKIE['user']);

}