{"id":169366,"date":"2024-05-08T16:44:36","date_gmt":"2024-05-08T08:44:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/169366.html"},"modified":"2024-05-08T16:44:39","modified_gmt":"2024-05-08T08:44:39","slug":"javascript-%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae-cookie","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/169366.html","title":{"rendered":"javascript \u5982\u4f55\u8bbe\u7f6e cookie"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27035737\/f48371eb-0413-44f7-ba41-92fb87ed68f8.webp\" alt=\"javascript \u5982\u4f55\u8bbe\u7f6e cookie\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\u8bbe\u7f6ecookie\u662f\u4e00\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u64cd\u4f5c\uff0c\u80fd\u591f\u8ba9\u5f00\u53d1\u8005\u5728\u7528\u6237\u7684\u6d4f\u89c8\u5668\u4e0a\u5b58\u50a8\u5c0f\u7247\u6bb5\u7684\u6570\u636e\u3002\u5176\u6838\u5fc3\u89c2\u70b9\u4e3b\u8981\u5305\u542b\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<strong>\u521b\u5efacookie\u3001\u8bbe\u7f6ecookie\u7684\u8fc7\u671f\u65f6\u95f4\u3001\u4e3acookie\u8bbe\u5b9a\u8def\u5f84\u9650\u5236<\/strong>\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecd<strong>\u521b\u5efacookie<\/strong>\u7684\u7ec6\u8282\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efacookie\u9996\u5148\u9700\u8981\u4e86\u89e3cookie\u7684\u57fa\u672c\u7ed3\u6784\uff0c\u5b83\u901a\u5e38\u5305\u542b\u4e00\u4e2a\u540d\u79f0\u3001\u4e00\u4e2a\u503c\u4ee5\u53ca\u4e00\u4e9b\u53ef\u9009\u7684\u5c5e\u6027\uff0c\u5982\u8fc7\u671f\u65f6\u95f4\u3001\u8def\u5f84\u3001\u57df\u7b49\u3002\u5728JavaScript\u4e2d\uff0c\u521b\u5efacookie\u7684\u57fa\u672c\u8bed\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4e3a<code>document.cookie<\/code>\u5c5e\u6027\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u8981\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cuser\u201d\u7684cookie\uff0c\u5e76\u5c06\u5176\u503c\u8bbe\u7f6e\u4e3a\u201cJohn Doe\u201d\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u7f16\u5199\u5982\u4e0b\u4ee3\u7801\uff1a<code>document.cookie = &quot;user=John Doe&quot;;<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u521b\u5efa\u7684cookie\u9ed8\u8ba4\u5728\u6d4f\u89c8\u5668\u4f1a\u8bdd\u7ed3\u675f\u65f6\u8fc7\u671f\uff0c\u5e76\u4e14\u6ca1\u6709\u8def\u5f84\u3001\u57df\u6216\u5176\u4ed6\u5c5e\u6027\u7684\u9650\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efaCOOKIE<\/h3>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\u521b\u5efacookie\u5f00\u59cb\u4e8e\u4e3a<code>document.cookie<\/code>\u5c5e\u6027\u8d4b\u4e88\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5e94\u9075\u5faa\u4e00\u5b9a\u7684\u683c\u5f0f\uff0c\u5373<code>key=value<\/code>\u5f62\u5f0f\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u503c\u4e2d\u5305\u542b\u7a7a\u683c\u6216\u8005\u5176\u4ed6\u9700\u8981\u7f16\u7801\u7684\u5b57\u7b26\uff0c\u5e94\u4f7f\u7528<code>encodeURIComponent<\/code>\u5bf9\u8fd9\u4e9b\u5b57\u7b26\u8fdb\u884c\u7f16\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.cookie = &quot;userName=&quot; + encodeURIComponent(&quot;John Doe&quot;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efacookie\u65f6\u8fd8\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u53ef\u9009\u5c5e\u6027\uff0c\u5982<code>expires<\/code>\u3001<code>max-age<\/code>\u3001<code>path<\/code>\u3001<code>dom<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n<\/code>\u3001<code>secure<\/code>\u7b49\uff0c\u800c\u8fd9\u4e9b\u5c5e\u6027\u53ef\u4ee5\u901a\u8fc7\u5728cookie\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u5bf9\u5e94\u7684\u5c5e\u6027\u503c\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u8981\u8bbe\u5b9a\u4e00\u4e2a\u8fc7\u671f\u65f6\u95f4\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var expiresDate = new Date();<\/p>\n<p>expiresDate.setTime(expiresDate.getTime() + (7 * 24 * 60 * 60 * 1000)); \/\/ \u8bbe\u7f6e7\u5929\u540e\u8fc7\u671f<\/p>\n<p>document.cookie = &quot;userName=John Doe; expires=&quot; + expiresDate.toUTCString();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff0c\u53ef\u4ee5\u63a7\u5236cookie\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6eCOOKIE\u7684\u8fc7\u671f\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>cookie\u7684\u8fc7\u671f\u65f6\u95f4\u662f\u51b3\u5b9acookie\u5b58\u50a8\u65f6\u957f\u7684\u91cd\u8981\u5c5e\u6027\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u8fc7\u671f\u65f6\u95f4\uff0c\u5219cookie\u5728\u6d4f\u89c8\u5668\u5173\u95ed\u65f6\u81ea\u52a8\u5220\u9664\u3002\u4e3a\u4e86\u8ba9cookie\u6301\u4e45\u4fdd\u5b58\u5728\u7528\u6237\u7684\u6d4f\u89c8\u5668\u4e2d\uff0c\u5fc5\u987b\u663e\u5f0f\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u6216\u8005\u6709\u6548\u671f\uff08max-age\uff09\u3002<\/p>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>expires<\/code>\u5c5e\u6027\u6765\u8bbe\u7f6e\u4e00\u4e2a\u5177\u4f53\u7684\u8fc7\u671f\u65e5\u671f\uff0c\u5982\u4e0a\u6240\u793a\u8bbe\u7f6e\u4e3a7\u5929\u540e\u3002\u6b64\u5916\uff0c<code>max-age<\/code>\u5c5e\u6027\u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u8bbe\u7f6ecookie\u6709\u6548\u671f\u7684\u65b9\u6cd5\uff0c\u5176\u503c\u4e3a\u4ece\u5f53\u524d\u65f6\u95f4\u5f00\u59cb\uff0ccookie\u5b58\u5728\u7684\u79d2\u6570\u3002\u4f8b\u5982\uff0c\u5c06cookie\u8bbe\u7f6e\u4e3a24\u5c0f\u65f6\u540e\u8fc7\u671f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.cookie = &quot;user=John Doe; max-age=&quot; + 60 * 60 * 24;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5408\u7406\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff0c\u53ef\u4ee5\u63a7\u5236cookie\u7684\u751f\u547d\u5468\u671f\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u4e1a\u52a1\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4e3aCOOKIE\u8bbe\u5b9a\u8def\u5f84\u9650\u5236<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efacookie\u65f6\uff0c\u53ef\u4ee5\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u8def\u5f84\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u9650\u5236\u53ea\u6709\u5728\u7279\u5b9a\u8def\u5f84\u4e0b\u7684\u9875\u9762\u624d\u80fd\u8bbf\u95ee\u5230\u8fd9\u4e2acookie\u3002\u8fd9\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u589e\u5f3a\u4e86cookie\u7684\u5b89\u5168\u6027\uff0c\u5e76\u4e14\u53ef\u4ee5\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u7f51\u7edc\u4f20\u8f93\u3002<\/p>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u8def\u5f84\uff0ccookie\u5c06\u5bf9\u5f53\u524d\u6587\u6863\u7684\u8def\u5f84\u53ca\u5176\u5b50\u8def\u5f84\u53ef\u89c1\u3002\u901a\u8fc7<code>path<\/code>\u5c5e\u6027\uff0c\u53ef\u4ee5\u66f4\u7ec6\u81f4\u5730\u63a7\u5236cookie\u7684\u53ef\u89c1\u8303\u56f4\u3002\u4f8b\u5982\uff0c\u4ec5\u5141\u8bb8\u6839\u8def\u5f84\u4e0b\u7684\u9875\u9762\u8bbf\u95eecookie\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.cookie = &quot;user=John Doe; path=\/&quot;;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bbe\u7f6e\u8def\u5f84\u9650\u5236\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u5b9e\u8df5\uff0c\u7279\u522b\u662f\u5728\u6784\u5efa\u5927\u578bweb\u5e94\u7528\u65f6\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ec4\u7ec7\u548c\u63a7\u5236\u4e0d\u540c\u90e8\u5206\u7684cookie\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8bbe\u7f6eCOOKIE\u7684\u5b89\u5168\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u901a\u8fc7cookie\u4f20\u8f93\u654f\u611f\u4fe1\u606f\u7684\u5e94\u7528\u6765\u8bf4\uff0c\u4fdd\u8bc1cookie\u7684\u5b89\u5168\u6027\u5c24\u4e3a\u91cd\u8981\u3002\u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>secure<\/code>\u548c<code>SameSite<\/code>\u5c5e\u6027\u6765\u589e\u5f3acookie\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5c06<code>secure<\/code>\u5c5e\u6027\u8bbe\u7f6e\u4e3atrue\uff0c\u53ef\u4ee5\u786e\u4fddcookie\u4ec5\u901a\u8fc7HTTPS\u534f\u8bae\u4f20\u8f93\uff0c\u4ece\u800c\u51cf\u5c11\u4e2d\u95f4\u4eba\u653b\u51fb\u7684\u98ce\u9669\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.cookie = &quot;user=John Doe; secure&quot;;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>SameSite<\/code>\u5c5e\u6027\u53ef\u4ee5\u5e2e\u52a9\u9632\u6b62\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff08CSRF\uff09\u653b\u51fb\u3002\u5b83\u9650\u5236\u4e86cookie\u80fd\u591f\u53d1\u9001\u7684\u8bf7\u6c42\u7c7b\u578b\uff0c\u53ef\u9009\u503c\u6709<code>Strict<\/code>\u3001<code>Lax<\/code>\u548c<code>None<\/code>\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6e\u4e3a<code>Strict<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.cookie = &quot;user=John Doe; SameSite=Strict&quot;;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u8fd9\u4e9b\u5b89\u5168\u5c5e\u6027\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u5e94\u7528\u7684\u5b89\u5168\u6027\uff0c\u4fdd\u62a4\u7528\u6237\u6570\u636e\u4e0d\u88ab\u6cc4\u9732\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5220\u9664COOKIE<\/h3>\n<\/p>\n<p><p>\u6700\u540e\u4f46\u5e76\u975e\u6700\u4e0d\u91cd\u8981\u7684\u662f\uff0c\u4e86\u89e3\u5982\u4f55\u5220\u9664cookie\u4e5f\u662f\u5fc5\u8981\u7684\u3002\u5220\u9664cookie\u5b9e\u9645\u4e0a\u662f\u901a\u8fc7\u8bbe\u7f6e\u5176\u8fc7\u671f\u65f6\u95f4\u4e3a\u8fc7\u53bb\u7684\u67d0\u4e2a\u65f6\u95f4\u6765\u5b9e\u73b0\u7684\u3002\u4f8b\u5982\uff0c\u8981\u5220\u9664\u540d\u4e3a\u201cuser\u201d\u7684cookie\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.cookie = &quot;user=; expires=Thu, 01 Jan 1970 00:00:00 GMT&quot;;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u663e\u5f0f\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u4e3a\u8fc7\u53bb\u7684\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u6d4f\u89c8\u5668\u5220\u9664\u5bf9\u5e94\u7684cookie\uff0c\u4ece\u800c\u5b9e\u73b0\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>JavaScript\u4e2d\u5173\u4e8ecookie\u7684\u64cd\u4f5c\u867d\u7136\u7b80\u5355\uff0c\u4f46\u638c\u63e1\u5176\u7ec6\u8282\u5e76\u5408\u7406\u5229\u7528\u53ef\u5927\u5927\u63d0\u5347Web\u5e94\u7528\u7684\u7528\u6237\u4f53\u9a8c\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528 JavaScript \u8bbe\u7f6e\u4e00\u4e2a\u7b80\u5355\u7684 cookie\uff1f<\/strong> <\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 <code>document.cookie<\/code> \u5c5e\u6027\u6765\u8bbe\u7f6e\u4e00\u4e2a\u7b80\u5355\u7684 cookie\u3002\u4f8b\u5982\uff0c <code>document.cookie = &quot;name=value; expires=date; path=path;&quot;<\/code>\u3002\u5176\u4e2d\uff0c<code>name<\/code> \u4ee3\u8868 cookie \u7684\u540d\u79f0\uff0c<code>value<\/code> \u4ee3\u8868 cookie \u7684\u503c\uff0c\u800c <code>expires<\/code> \u662f cookie \u7684\u8fc7\u671f\u65f6\u95f4\uff0c<code>path<\/code> \u5219\u6307\u5b9a cookie \u7684\u8def\u5f84\u3002<\/p>\n<p><strong>2. \u6211\u8be5\u5982\u4f55\u8bbe\u7f6e\u4e00\u4e2a\u5e26\u6709\u8fc7\u671f\u65f6\u95f4\u7684 cookie\uff1f<\/strong> <\/p>\n<p>\u8981\u8bbe\u7f6e\u4e00\u4e2a\u6709\u8fc7\u671f\u65f6\u95f4\u7684 cookie\uff0c\u5728 <code>expires<\/code> \u5c5e\u6027\u4e2d\u6307\u5b9a\u4e00\u4e2a\u65e5\u671f\u5373\u53ef\uff0c\u683c\u5f0f\u4e3a &quot;Day, DD Mon YYYY HH:MM:SS GMT&quot;\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u8bbe\u7f6e\u4e00\u4e2a\u8fc7\u671f\u65f6\u95f4\u4e3a\u4e00\u5468\u540e\u7684 cookie\uff1a<code>document.cookie = &quot;name=value; expires=Sat, 14 Aug 2022 12:00:00 GMT; path=\/&quot;;<\/code>\u3002<\/p>\n<p><strong>3. \u6211\u53ef\u4ee5\u901a\u8fc7 JavaScript \u8bbe\u7f6e\u5728\u6574\u4e2a\u7f51\u7ad9\u8303\u56f4\u5185\u90fd\u53ef\u8bbf\u95ee\u7684 cookie \u5417\uff1f<\/strong> <\/p>\n<p>\u662f\u7684\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>path<\/code> \u5c5e\u6027\u6765\u5b9e\u73b0\u5728\u6574\u4e2a\u7f51\u7ad9\u8303\u56f4\u5185\u90fd\u53ef\u4ee5\u8bbf\u95ee\u7684 cookie\u3002\u5c06 <code>path<\/code> \u7684\u503c\u8bbe\u7f6e\u4e3a &quot;\/&quot;\uff0c\u5c31\u53ef\u4ee5\u5c06 cookie \u8bbe\u4e3a\u5168\u7ad9\u53ef\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c<code>document.cookie = &quot;name=value; expires=date; path=\/&quot;;<\/code>\u3002\u8fd9\u6837\uff0c\u5728\u6574\u4e2a\u7f51\u7ad9\u7684\u4efb\u4f55\u9875\u9762\u4e0a\u90fd\u53ef\u4ee5\u8bbf\u95ee\u5230\u8be5 cookie\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\u8bbe\u7f6ecookie\u662f\u4e00\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u64cd\u4f5c\uff0c\u80fd\u591f\u8ba9\u5f00\u53d1\u8005\u5728\u7528\u6237\u7684\u6d4f\u89c8\u5668\u4e0a\u5b58\u50a8\u5c0f\u7247\u6bb5\u7684\u6570\u636e\u3002\u5176 [&hellip;]","protected":false},"author":3,"featured_media":169374,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/169366"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=169366"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/169366\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/169374"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=169366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=169366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=169366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}