{"id":46964,"date":"2024-12-02T11:13:18","date_gmt":"2024-12-02T03:13:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46964\/"},"modified":"2024-12-02T11:13:18","modified_gmt":"2024-12-02T03:13:18","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e5%9c%a8-web-%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e4%b8%ad%e4%bd%bf%e7%94%a8-cookie%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46964\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 cookie\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 cookie\uff1f<\/h1>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aPHP \u51fd\u6570\u5982\u4f55\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 cookie\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>PHP \u4e2d Cookie \u7684\u7528\u6cd5\u5305\u62ec\uff1a\u8bbe\u7f6e Cookie\uff1a\u4f7f\u7528 setcookie() \u51fd\u6570\u8bbe\u7f6e\u540d\u79f0\u3001\u503c\u3001\u8fc7\u671f\u65f6\u95f4\u548c\u8def\u5f84\u3002\u83b7\u53d6 Cookie\uff1a\u4f7f\u7528 $_COOKIE \u6570\u7ec4\u83b7\u53d6 Cookie \u503c\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u8ddf\u8e2a\u7528\u6237\u4f1a\u8bdd\uff0c\u901a\u8fc7\u8bbe\u7f6e Cookie \u6807\u8bc6\u5df2\u767b\u5f55\u7528\u6237\u5e76\u5728\u5176\u4ed6\u9875\u9762\u68c0\u67e5\u5176\u5b58\u5728\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729857570671b8822b4cf9.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 cookie\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 cookie\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u4e2d Cookie \u7684\u7528\u6cd5<\/strong><\/p>\n<p>\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0cCookie \u662f\u4e00\u79cd\u5c0f\u5757\u6570\u636e\uff0c\u5b58\u50a8\u5728\u5ba2\u6237\u7aef\uff08\u901a\u5e38\u662f\u5728\u6d4f\u89c8\u5668\u7684 cookie \u5b58\u50a8\u4e2d\uff09\u5e76\u5728\u968f\u540e\u7684\u8bf7\u6c42\u4e2d\u53d1\u9001\u56de\u670d\u52a1\u5668\u3002\u5b83\u4eec\u53ef\u7528\u4e8e\u8ddf\u8e2a\u7528\u6237\u4f1a\u8bdd\u3001\u4e2a\u6027\u5316\u5185\u5bb9\u6216\u5b58\u50a8\u7528\u6237\u9996\u9009\u9879\u3002<\/p>\n<p><strong>\u8bbe\u7f6e Cookie<\/strong><\/p>\n<p>\u4ee5\u4e0b PHP \u4ee3\u7801\u521b\u5efa\u5e76\u8bbe\u7f6e\u4e00\u4e2a\u540d\u4e3a &#8220;username&#8221; \u7684 cookie\uff0c\u5176\u4e2d\u5305\u542b\u503c &#8220;JohnDoe&#8221;\uff1a<\/p>\n<pre>setcookie(\"username\", \"JohnDoe\", time() + (86400 * 30), \"\/\"); \/\/ \u5c06 cookie \u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u6708\u540e\u8fc7\u671f<\/pre>\n<p>\u53c2\u6570\u4f9d\u6b21\u4e3a\uff1a<\/p>\n<ul>\n<li>cookie \u540d\u79f0<\/li>\n<li>cookie \u503c<\/li>\n<li>\u8fc7\u671f\u65f6\u95f4\u6233\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff09<\/li>\n<li>cookie \u8def\u5f84\uff08\u53ef\u9009\u62e9\uff09<\/li>\n<\/ul>\n<p><strong>\u83b7\u53d6 Cookie<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u83b7\u53d6\u5e76\u6253\u5370\u540d\u4e3a &#8220;username&#8221; \u7684 cookie \u7684\u503c\uff1a<\/p>\n<pre>if (isset($_COOKIE[\"username\"])) {\n    echo $_COOKIE[\"username\"];\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u7528\u6237\u4f1a\u8bdd\u8ddf\u8e2a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\u6f14\u793a\u5982\u4f55\u4f7f\u7528 Cookie \u8ddf\u8e2a\u7528\u6237\u4f1a\u8bdd\uff1a<\/p>\n<pre>&lt;?php\n\/\/ \u5728\u767b\u5f55\u9875\u9762\uff1a\nif (!isset($_COOKIE[\"user_id\"])) {\n    \/\/ \u7528\u6237\u672a\u767b\u5f55\uff0c\u91cd\u5b9a\u5411\u5230\u767b\u5f55\u9875\u9762\n    header(\"Location: login.php\");\n}\n\n\/\/ \u5728\u5176\u4ed6\u9875\u9762\uff1a\nif (isset($_COOKIE[\"user_id\"])) {\n    \/\/ \u7528\u6237\u5df2\u767b\u5f55\uff0c\u663e\u793a\u6b22\u8fce\u6d88\u606f\n    echo \"\u6b22\u8fce\u56de\u6765\uff0c\" . $_COOKIE[\"username\"] . \"\uff01\";\n}\n?&gt;<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53\u7528\u6237\u767b\u5f55\u65f6\uff0c\u4fbf\u4f1a\u8bbe\u7f6e &#8220;user_id&#8221; cookie\u3002\u5f53\u7528\u6237\u6d4f\u89c8\u5176\u4ed6\u9875\u9762\u65f6\uff0c\u4f1a\u68c0\u67e5\u8be5 cookie \u4ee5\u786e\u5b9a\u4ed6\u4eec\u662f\u5426\u4ecd\u5df2\u767b\u5f55\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>Cookie \u6570\u636e\u5bb9\u6613\u53d7\u5230\u8de8\u7f51\u7ad9\u811a\u672c (XSS) \u653b\u51fb\uff0c\u56e0\u6b64\u5fc5\u987b\u5c0f\u5fc3\u5904\u7406\u3002<\/li>\n<li>\u5e94\u4f7f\u7528 HTTPS \u534f\u8bae\u4f20\u9012 Cookie \u4ee5\u786e\u4fdd\u5b89\u5168\u3002<\/li>\n<li>\u907f\u514d\u5b58\u50a8\u654f\u611f\u4fe1\u606f\uff08\u5982\u5bc6\u7801\uff09\u5728 Cookie \u4e2d\u3002<\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPHP \u51fd\u6570\u5982\u4f55\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 cookie\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8ephp,cookie\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 cookie\uff1f \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aPHP \u51fd\u6570\u5982\u4f55\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 cookie\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 PHP \u4e2d Cookie \u7684\u7528\u6cd5\u5305\u62ec\uff1a\u8bbe\u7f6e Cookie\uff1a\u4f7f\u7528 setcookie() \u51fd\u6570\u8bbe\u7f6e\u540d\u79f0\u3001\u503c\u3001\u8fc7\u671f\u65f6\u95f4\u548c\u8def\u5f84\u3002\u83b7\u53d6 Cookie\uff1a\u4f7f\u7528 $_COOKIE \u6570\u7ec4\u83b7\u53d6 Cookie \u503c\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u8ddf\u8e2a\u7528\u6237\u4f1a\u8bdd\uff0c\u901a\u8fc7\u8bbe\u7f6e Cookie \u6807\u8bc6\u5df2\u767b\u5f55\u7528\u6237\u5e76\u5728\u5176\u4ed6\u9875\u9762\u68c0\u67e5\u5176\u5b58\u5728\u3002 PHP \u4e2d Cookie \u7684\u7528\u6cd5 \u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0cCookie \u662f\u4e00\u79cd\u5c0f\u5757\u6570\u636e\uff0c\u5b58\u50a8\u5728\u5ba2\u6237\u7aef\uff08\u901a\u5e38\u662f\u5728\u6d4f\u89c8\u5668\u7684 cookie \u5b58\u50a8\u4e2d\uff09\u5e76\u5728\u968f\u540e\u7684\u8bf7\u6c42\u4e2d\u53d1\u9001\u56de\u670d\u52a1\u5668\u3002\u5b83\u4eec\u53ef\u7528\u4e8e\u8ddf\u8e2a\u7528\u6237\u4f1a\u8bdd\u3001\u4e2a\u6027\u5316\u5185\u5bb9\u6216\u5b58\u50a8\u7528\u6237\u9996\u9009\u9879\u3002 \u8bbe\u7f6e Cookie \u4ee5\u4e0b PHP \u4ee3\u7801\u521b\u5efa\u5e76\u8bbe\u7f6e\u4e00\u4e2a\u540d\u4e3a &#8220;username&#8221; \u7684 cookie\uff0c\u5176\u4e2d\u5305\u542b\u503c &#8220;JohnDoe&#8221;\uff1a setcookie(&#8220;username&#8221;, &#8220;JohnDoe&#8221;, time() + (86400 * 30), &#8220;\/&#8221;); \/\/ \u5c06 cookie \u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u6708\u540e\u8fc7\u671f [&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-46964","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46964","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=46964"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46964\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}