{"id":22989,"date":"2024-11-21T14:55:35","date_gmt":"2024-11-21T06:55:35","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22989\/"},"modified":"2024-11-21T14:55:35","modified_gmt":"2024-11-21T06:55:35","slug":"laravel%e4%b8%ad%e8%ae%be%e7%bd%aecookie%e5%9f%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22989\/","title":{"rendered":"laravel\u4e2d\u8bbe\u7f6ecookie\u57df"},"content":{"rendered":"<p>\u5982\u679c\u60a8\u6b63\u5728\u4f7f\u7528 laravel \u6846\u67b6\u5f00\u53d1\u7f51\u7ad9\u5e94\u7528\u7a0b\u5e8f\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e cookie \u57df\u6765\u786e\u4fdd\u60a8\u7684 cookie \u9002\u7528\u4e8e\u6307\u5b9a\u7684\u57df\u540d\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u5728 laravel \u4e2d\u8bbe\u7f6e cookie \u57df\u3002<\/p>\n<p>\u4ec0\u4e48\u662f Cookie \u57df<\/p>\n<p>Cookie \u57df\u662f\u6307 Cookie \u53ef\u7528\u7684\u57df\u540d\u6216\u5b50\u57df\u540d\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u8bbe\u7f6e\u4e86\u9002\u7528\u4e8e &#8220;.example.com&#8221; \u57df\u7684 Cookie\uff0c\u90a3\u4e48\u8fd9\u4e2a Cookie \u53ef\u4ee5\u5728 example.com \u7684\u4efb\u4f55\u5b50\u57df\u540d\u4e0b\u4f7f\u7528\uff0c\u4f8b\u5982 blog.example.com \u548c shop.example.com\u3002\u5982\u679c\u60a8\u8bbe\u7f6e\u7684 Cookie \u57df\u4e3a &#8220;example.com&#8221;\uff0c\u5219\u8fd9\u4e2a Cookie \u53ea\u80fd\u5728 example.com \u7684\u6839\u57df\u540d\u4e0b\u4f7f\u7528\u3002<\/p>\n<p>\u5728 Laravel \u4e2d\u8bbe\u7f6e Cookie \u57df<\/p>\n<p>\u5728 Laravel \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 cookie \u5168\u5c40\u52a9\u624b\u548c withCookie \u65b9\u6cd5\u6765\u8bbe\u7f6e\u5e94\u7528\u7a0b\u5e8f\u7684 Cookie \u57df\u3002<\/p>\n<p>\u8981\u8bbe\u7f6e Cookie \u57df\uff0c\u8bf7\u4f7f\u7528 cookie \u5168\u5c40\u52a9\u624b\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u7684 Cookie\uff0c\u5305\u62ec\u60a8\u8981\u4f7f\u7528\u7684\u57df\u540d\uff1a<\/p>\n<pre>$response = response('Hello World')-&amp;gt;cookie(\n    'name', 'value', $minutes, '\/', '.example.com', false, true\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u4ee3\u7801\u5c06\u5728 &#8220;.example.com&#8221; \u57df\u4e0b\u8bbe\u7f6e\u4e00\u4e2a\u540d\u4e3a &#8220;name&#8221; \u503c\u4e3a &#8220;value&#8221;\uff0c\u8fc7\u671f\u65f6\u95f4\u4e3a $minutes \u5206\u949f\u7684 Cookie\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 withCookie \u65b9\u6cd5\u5c06\u4e0a\u8ff0\u4ee3\u7801\u5e94\u7528\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u4e2d\uff1a<\/p>\n<pre>return response('Hello World')-&amp;gt;withCookie($cookie);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8bf7\u6ce8\u610f\uff0cwithCookie \u65b9\u6cd5\u4e5f\u53ef\u4ee5\u4e00\u6b21\u8bbe\u7f6e\u591a\u4e2a Cookie\uff1a<\/p>\n<pre>return response('Hello World')-&amp;gt;withCookie($cookie1, $cookie2, $cookie3);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u793a\u4f8b<\/p>\n<pre>$cookie = cookie('name', 'value', 60);\nreturn response('Hello World')-&amp;gt;withCookie($cookie);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u5728\u5f53\u524d\u57df\u540d\u4e0b\u8bbe\u7f6e\u4e00\u4e2a\u540d\u4e3a &#8220;name&#8221; \u503c\u4e3a &#8220;value&#8221;\uff0c\u8fc7\u671f\u65f6\u95f4\u4e3a 60 \u5206\u949f\u7684 Cookie\u3002<\/p>\n<p>\u5982\u679c\u60a8\u60f3\u5c06 Cookie \u57df\u9650\u5236\u5728\u5f53\u524d\u6839\u57df\u540d\u4e0b\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre>$response = response('Hello World')-&amp;gt;cookie(\n    'name', 'value', $minutes, '\/'\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u5728\u5f53\u524d\u57df\u540d\u7684\u6839\u76ee\u5f55\u4e0b\u8bbe\u7f6e\u4e00\u4e2a Cookie\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u5728 Laravel \u4e2d\u8bbe\u7f6e Cookie \u57df\u9700\u8981\u5b9a\u4e49\u8981\u4f7f\u7528\u7684\u57df\u540d\u548c\u5b50\u57df\u540d\u5217\u8868\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 cookie \u4e0e withCookie \u65b9\u6cd5\u6765\u8f7b\u677e\u5730\u8bbe\u7f6e\u5e94\u7528\u7a0b\u5e8f\u7684 Cookie\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8bbe\u7f6e Cookie \u57df\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u7684\u7ba1\u7406 Laravel \u4e2d\u7684 Cookie \u57df\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u4e2d\u8bbe\u7f6ecookie\u57df\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u60a8\u6b63\u5728\u4f7f\u7528 laravel \u6846\u67b6\u5f00\u53d1\u7f51\u7ad9\u5e94\u7528\u7a0b\u5e8f\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e cookie \u57df\u6765\u786e\u4fdd\u60a8\u7684 cookie \u9002\u7528\u4e8e\u6307\u5b9a\u7684\u57df\u540d\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u5728 laravel \u4e2d\u8bbe\u7f6e cookie \u57df\u3002 \u4ec0\u4e48\u662f Cookie \u57df Cookie \u57df\u662f\u6307 Cookie \u53ef\u7528\u7684\u57df\u540d\u6216\u5b50\u57df\u540d\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u8bbe\u7f6e\u4e86\u9002\u7528\u4e8e &#8220;.example.com&#8221; \u57df\u7684 Cookie\uff0c\u90a3\u4e48\u8fd9\u4e2a Cookie \u53ef\u4ee5\u5728 example.com \u7684\u4efb\u4f55\u5b50\u57df\u540d\u4e0b\u4f7f\u7528\uff0c\u4f8b\u5982 blog.example.com \u548c shop.example.com\u3002\u5982\u679c\u60a8\u8bbe\u7f6e\u7684 Cookie \u57df\u4e3a &#8220;example.com&#8221;\uff0c\u5219\u8fd9\u4e2a Cookie \u53ea\u80fd\u5728 example.com \u7684\u6839\u57df\u540d\u4e0b\u4f7f\u7528\u3002 \u5728 Laravel \u4e2d\u8bbe\u7f6e Cookie \u57df \u5728 Laravel \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 cookie \u5168\u5c40\u52a9\u624b\u548c withCookie \u65b9\u6cd5\u6765\u8bbe\u7f6e\u5e94\u7528\u7a0b\u5e8f\u7684 Cookie \u57df\u3002 \u8981\u8bbe\u7f6e Cookie \u57df\uff0c\u8bf7\u4f7f\u7528 cookie \u5168\u5c40\u52a9\u624b\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u7684 Cookie\uff0c\u5305\u62ec\u60a8\u8981\u4f7f\u7528\u7684\u57df\u540d\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22989","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22989","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22989"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22989\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}