{"id":23682,"date":"2024-11-21T09:41:27","date_gmt":"2024-11-21T01:41:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23682\/"},"modified":"2024-11-21T09:41:27","modified_gmt":"2024-11-21T01:41:27","slug":"%e5%9c%a8thinkphp6%e4%b8%ad%e4%bd%bf%e7%94%a8cookie%e6%8a%80%e6%9c%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23682\/","title":{"rendered":"\u5728ThinkPHP6\u4e2d\u4f7f\u7528Cookie\u6280\u672f"},"content":{"rendered":"<p>\u4f5c\u4e3a\u4e00\u79cd\u5e38\u89c1\u7684\u72b6\u6001\u7ba1\u7406\u6280\u672f\uff0ccookie\u5df2\u7ecf\u5e7f\u6cdb\u5e94\u7528\u4e8eweb\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u4e2d\u3002\u5b83\u53ef\u4ee5\u4fdd\u5b58\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\uff0c\u4f8b\u5982\u7528\u6237\u540d\u3001\u8d2d\u7269\u8f66\u6570\u636e\u7b49\u7b49\uff0c\u4ece\u800c\u5728\u4e0d\u540c\u7684\u9875\u9762\u4e4b\u95f4\u4f20\u9012\u4fe1\u606f\u3002\u57286\u4e2d\uff0c\u4f7f\u7528cookie\u6280\u672f\u4e5f\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002<\/p>\n<ol>\n<li>\u8bbe\u7f6eCookie<\/li>\n<\/ol>\n<p>\u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Cookie\u7c7b\u6765\u8bbe\u7f6eCookie\u3002\u8be5\u7c7b\u5c01\u88c5\u4e86\u4e00\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbe\u7f6e\u3001\u83b7\u53d6\u548c\u5220\u9664Cookie\u3002<\/p>\n<pre>use thinkacadeCookie;\n\n\/\/ \u8bbe\u7f6e\u4e00\u4e2a\u540d\u4e3ausername\u7684Cookie\uff0c\u503c\u4e3aadmin\uff0c\u6709\u6548\u671f\u4e3a1\u5929\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff09\nCookie::set('username', 'admin', 86400);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cset() \u65b9\u6cd5\u6709\u4e09\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u4e3aCookie\u7684\u540d\u79f0\u3001\u503c\u548c\u6709\u6548\u671f\u3002\u5176\u4e2d\uff0c\u6709\u6548\u671f\u4e3a\u53ef\u9009\u53c2\u6570\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5219\u9ed8\u8ba4\u4e3a\u5173\u95ed\u6d4f\u89c8\u5668\u65f6\u8fc7\u671f\u3002<\/p>\n<ol>\n<li>\u8bfb\u53d6Cookie<\/li>\n<\/ol>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528get()\u65b9\u6cd5\u6765\u8bfb\u53d6Cookie\u7684\u503c\uff1a<\/p>\n<pre>use thinkacadeCookie;\n\n\/\/ \u8bfb\u53d6\u540d\u4e3ausername\u7684Cookie\u7684\u503c\n$username = Cookie::get('username');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679cCookie\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56denull\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u5220\u9664Cookie<\/li>\n<\/ol>\n<p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5220\u9664\u4e00\u4e2aCookie\uff0c\u53ef\u4ee5\u4f7f\u7528delete()\u65b9\u6cd5\uff1a<\/p>\n<pre>use thinkacadeCookie;\n\n\/\/ \u5220\u9664\u540d\u4e3ausername\u7684Cookie\nCookie::delete('username');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>Cookie\u7684\u9009\u9879<\/li>\n<\/ol>\n<p>\u5728\u8bbe\u7f6eCookie\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7b2c\u56db\u4e2a\u53c2\u6570\u6765\u8bbe\u7f6eCookie\u7684\u9009\u9879\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>use thinkacadeCookie;\n\n\/\/ \u8bbe\u7f6e\u540d\u4e3atest\u7684Cookie\uff0c\u503c\u4e3a123\uff0c\u6709\u6548\u671f\u4e3a1\u5c0f\u65f6\uff0c\u53ea\u80fd\u901a\u8fc7HTTPS\u534f\u8bae\u4f20\u8f93\nCookie::set('test', '123', 3600, [\n    'httponly' =&amp;gt; true,\n    'secure'   =&amp;gt; true,\n]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u4e24\u4e2a\u9009\u9879\uff1ahttponly\u548csecure\u3002httponly\u9009\u9879\u53ef\u4ee5\u9632\u6b62\u5728JavaScript\u4e2d\u8bbf\u95eeCookie\uff0c\u4ece\u800c\u589e\u52a0\u5b89\u5168\u6027\uff1bsecure\u9009\u9879\u8868\u793a\u53ea\u6709\u5728HTTPS\u534f\u8bae\u4e0b\u624d\u80fd\u4f20\u8f93\u8be5Cookie\uff0c\u4e5f\u662f\u4e00\u79cd\u5b89\u5168\u63aa\u65bd\u3002<\/p>\n<ol>\n<li>\u5728View\u4e2d\u4f7f\u7528Cookie<\/li>\n<\/ol>\n<p>\u5728View\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528Cookie\u7c7b\u6765\u8bfb\u53d6Cookie\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/ \u83b7\u53d6\u540d\u4e3ausername\u7684Cookie\n$username =     hinkacadeCookie::get('username');\n\n\/\/ \u5728View\u4e2d\u8f93\u51faCookie\u7684\u503c\necho '\u6b22\u8fce\u60a8\uff0c' . $username;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528get()\u65b9\u6cd5\u83b7\u53d6\u4e86\u540d\u4e3ausername\u7684Cookie\u503c\uff0c\u7136\u540e\u5728View\u4e2d\u8f93\u51fa\u4e86\u5b83\u3002<\/p>\n<ol>\n<li>\u603b\u7ed3<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728ThinkPHP6\u4e2d\u4f7f\u7528Cookie\u6280\u672f\u7684\u65b9\u6cd5\u3002Cookie\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u5355\u3001\u6613\u4e8e\u4f7f\u7528\u7684\u72b6\u6001\u7ba1\u7406\u6280\u672f\uff0c\u4f46\u5728\u4f7f\u7528\u65f6\u9700\u8981\u6ce8\u610f\u5b89\u5168\u6027\u3002\u4e3a\u4e86\u9632\u6b62XSS\u7b49\u5b89\u5168\u95ee\u9898\uff0c\u6211\u4eec\u5e94\u8be5\u5c3d\u53ef\u80fd\u5730\u4f7f\u7528\u9009\u9879\u6765\u589e\u52a0Cookie\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728ThinkPHP6\u4e2d\u4f7f\u7528Cookie\u6280\u672f\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>\u4f5c\u4e3a\u4e00\u79cd\u5e38\u89c1\u7684\u72b6\u6001\u7ba1\u7406\u6280\u672f\uff0ccookie\u5df2\u7ecf\u5e7f\u6cdb\u5e94\u7528\u4e8eweb\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u4e2d\u3002\u5b83\u53ef\u4ee5\u4fdd\u5b58\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\uff0c\u4f8b\u5982\u7528\u6237\u540d\u3001\u8d2d\u7269\u8f66\u6570\u636e\u7b49\u7b49\uff0c\u4ece\u800c\u5728\u4e0d\u540c\u7684\u9875\u9762\u4e4b\u95f4\u4f20\u9012\u4fe1\u606f\u3002\u57286\u4e2d\uff0c\u4f7f\u7528cookie\u6280\u672f\u4e5f\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002 \u8bbe\u7f6eCookie \u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Cookie\u7c7b\u6765\u8bbe\u7f6eCookie\u3002\u8be5\u7c7b\u5c01\u88c5\u4e86\u4e00\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbe\u7f6e\u3001\u83b7\u53d6\u548c\u5220\u9664Cookie\u3002 use thinkacadeCookie; \/\/ \u8bbe\u7f6e\u4e00\u4e2a\u540d\u4e3ausername\u7684Cookie\uff0c\u503c\u4e3aadmin\uff0c\u6709\u6548\u671f\u4e3a1\u5929\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff09 Cookie::set(&#8216;username&#8217;, &#8216;admin&#8217;, 86400); \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cset() \u65b9\u6cd5\u6709\u4e09\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u4e3aCookie\u7684\u540d\u79f0\u3001\u503c\u548c\u6709\u6548\u671f\u3002\u5176\u4e2d\uff0c\u6709\u6548\u671f\u4e3a\u53ef\u9009\u53c2\u6570\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5219\u9ed8\u8ba4\u4e3a\u5173\u95ed\u6d4f\u89c8\u5668\u65f6\u8fc7\u671f\u3002 \u8bfb\u53d6Cookie \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528get()\u65b9\u6cd5\u6765\u8bfb\u53d6Cookie\u7684\u503c\uff1a use thinkacadeCookie; \/\/ \u8bfb\u53d6\u540d\u4e3ausername\u7684Cookie\u7684\u503c $username = Cookie::get(&#8216;username&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u5982\u679cCookie\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56denull\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5220\u9664Cookie \u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5220\u9664\u4e00\u4e2aCookie\uff0c\u53ef\u4ee5\u4f7f\u7528delete()\u65b9\u6cd5\uff1a use thinkacadeCookie; \/\/ \u5220\u9664\u540d\u4e3ausername\u7684Cookie Cookie::delete(&#8216;username&#8217;); \u767b\u5f55\u540e\u590d\u5236 Cookie\u7684\u9009\u9879 \u5728\u8bbe\u7f6eCookie\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7b2c\u56db\u4e2a\u53c2\u6570\u6765\u8bbe\u7f6eCookie\u7684\u9009\u9879\u3002\u4f8b\u5982\uff1a use thinkacadeCookie; \/\/ \u8bbe\u7f6e\u540d\u4e3atest\u7684Cookie\uff0c\u503c\u4e3a123\uff0c\u6709\u6548\u671f\u4e3a1\u5c0f\u65f6\uff0c\u53ea\u80fd\u901a\u8fc7HTTPS\u534f\u8bae\u4f20\u8f93 Cookie::set(&#8216;test&#8217;, &#8216;123&#8217;, 3600, [ &#8216;httponly&#8217; =&amp;gt; true, &#8216;secure&#8217; =&amp;gt; true, ]); \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u4e24\u4e2a\u9009\u9879\uff1ahttponly\u548csecure\u3002httponly\u9009\u9879\u53ef\u4ee5\u9632\u6b62\u5728JavaScript\u4e2d\u8bbf\u95eeCookie\uff0c\u4ece\u800c\u589e\u52a0\u5b89\u5168\u6027\uff1bsecure\u9009\u9879\u8868\u793a\u53ea\u6709\u5728HTTPS\u534f\u8bae\u4e0b\u624d\u80fd\u4f20\u8f93\u8be5Cookie\uff0c\u4e5f\u662f\u4e00\u79cd\u5b89\u5168\u63aa\u65bd\u3002 \u5728View\u4e2d\u4f7f\u7528Cookie \u5728View\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528Cookie\u7c7b\u6765\u8bfb\u53d6Cookie\u3002\u4f8b\u5982\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-23682","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23682","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=23682"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23682\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}