简体   繁体   English

如何在 Laravel 刀片文件中设置 cookie?

[英]How to set cookie in Laravel Blade File?

I want to set cookies in laravel blade.php file, not in the controller.我想在 laravel 刀片中设置 cookies How can I set it?我该如何设置?

It's front end.是前端。 I suggest using JavaScript for this: https://www.w3schools.com/js/js_cookies.asp我建议为此使用 JavaScript: https://www.w3schools.com/js/js_cookies.asp

Disclaimer : I will focus my answer on PHP and laravel.免责声明:我将把我的答案集中在 PHP 和 laravel 上。

Why not set in controller?为什么不在 controller 中设置?

It would really help to know why you cannot / or do not want to set cookies using laravel's cookie Facade in the controller - eg.知道为什么你不能/或不想在 controller 中使用laravel 的 cookie Facade设置 cookies 真的很有帮助 - 例如。 Cookie::queue , as it's very easy to do! Cookie::queue ,因为它很容易做到!

Here are two ways, from this source .这里有两种方法,来自这个来源

Via response: return response(view('welcome'))->cookie('name','value',$min);通过响应: return response(view('welcome'))->cookie('name','value',$min);

Via Queue: Cookie::queue(Cookie::make('name','value',$min)); return view('welcome');通过队列: Cookie::queue(Cookie::make('name','value',$min)); return view('welcome'); Cookie::queue(Cookie::make('name','value',$min)); return view('welcome');


Set-Cookie is a response header, not the body! Set-Cookie 是响应 header,而不是正文!

Assuming you would set these cookies in PHP , they need to come as part of a response header, and not part of the body (view).假设您将这些 cookies 设置在PHP中,它们需要作为响应 header 的一部分出现,而不是正文的一部分(视图)。 This is why you would need to set these in the controller, where you are sending a response!这就是为什么您需要在 controller 中设置这些,您将在其中发送响应!

If you try to use PHP functions to set cookies, you will be met with errors "headers have already been sent"如果您尝试使用 PHP 函数设置 cookies,您将遇到错误“headers have been sent”

Per the docs: https://www.php.net/setcookie根据文档: https://www.php.net/setcookie

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. setcookie() 定义要与 HTTP 标头的 rest 一起发送的 cookie。 Like other headers, cookies must be sent before any output from your script (this is a protocol restriction).与其他标头一样,cookies 必须在脚本中的任何 output 之前发送(这是协议限制)。 This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.这要求您在任何 output 之前调用此 function,包括和标签以及任何空格。

To understand what this means, it's helpful to understand the structure of requests and responses :要理解这意味着什么,了解请求响应的结构会很有帮助:

Requests and Responses are made up of headers and possibly a body . RequestsResponsesheaders可能的 body组成。

  • Note: You can see these in the network tab of your browser's dev tools.注意:您可以在浏览器开发工具的网络选项卡中看到这些。

The request headers are like meta data about the request that can tell the server what kind of content is being requested, and who is requesting.请求标头就像关于请求的元数据,可以告诉服务器正在请求什么样的内容,以及谁在请求。

The response headers are like meta data about the response returned that can tell the server what kind of content is being delivered, how long to cache it for, associated cookies that got set.响应标头就像有关返回响应的元数据,可以告诉服务器正在传递什么样的内容,缓存多长时间,关联的 cookies 已设置。

Example Request Headers:示例请求标头:

  • Content-Type: 'application/json'
  • Content-Type: 'application/pdf'
  • Content-Type: 'text/html'
  • Content-Type: 'text/css'
  • User-Agent: 'Mozilla/5.0 (<system-information>) <platform> (<platform-details>) <extensions>'
  • Authorization: 'Bearer <token>'

Example Response Headers:示例响应标头:

  • Content-Type as it may differ from what was requested Content-Type不同
  • Expires: 'Wed, 07 Sep 2022 19:26:49 GMT'
  • Cross-Origin-Resource-Policy: 'cross-origin'
  • Date: 'Wed, 07 Sep 2022 19:26:49 GMT'
  • Content-Length: 0 , Content-Length: 0
  • Set-Cookie: test_cookie=CheckForPermission; expires=Wed, 07-Sep-2022 19:41:49 GMT; path=/; domain=.doubleclick.net; Secure; HttpOnly; SameSite=none

Notably: - Set-Cookie - tells the browser to add these cookies to application storage (you can view these in application / storage tabs in dev tools)值得注意的是: - Set-Cookie - 告诉浏览器将这些 cookies 添加到应用程序存储(您可以在开发工具的应用程序/存储选项卡中查看这些)

The response header can have Set-Cookie , not the request header.响应 header 可以有Set-Cookie ,而不是请求 header。 This makes sense, as usually the cookie information is going to come from the "answer" (response) to the "question" (request) by way of performing some logic, eg - this user is authenticated, here's a cookie to keep their session in place.这是有道理的,因为通常 cookie 信息将通过执行某些逻辑从“答案”(响应)到“问题”(请求),例如 - 此用户已通过身份验证,这里有一个 cookie 来保存他们的 session到位。


Also: Secure & HTTP only Cookies另外:仅安全和 HTTP Cookies

Cookies can get set with a few options - secure only, and http only. Cookies 可以设置几个选项 - 仅安全,仅 http。 These mean that the cookie must be Set on secure connections (https) and the http only can come from a response and cannot be overridden by JavaScript adjusting (client side)这意味着必须在安全连接 (https) 上设置 cookie,并且 http 只能来自响应,不能被 JavaScript 调整覆盖(客户端)

Example of options for Laravel's Cookie::queue facade: Laravel 的 Cookie::queue 外观的选项示例:

// $name, $value, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true
Cookie::queue($name, $value, $ttl, $path, $domain, $secure, $httpOnly);

ttl = "time to live" or how long until it expires eg. ttl =“生存时间”或多久到期,例如。 2 minutes 2分钟

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM