{"id":3051,"date":"2025-03-20T23:56:28","date_gmt":"2025-03-20T15:56:28","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3051"},"modified":"2025-03-20T23:56:28","modified_gmt":"2025-03-20T15:56:28","slug":"php-cookie-%e5%85%a8%e9%9d%a2%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/20\/php-cookie-%e5%85%a8%e9%9d%a2%e8%af%a6%e8%a7%a3\/","title":{"rendered":"PHP Cookie \u5168\u9762\u8be6\u89e3"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1. \u4ec0\u4e48\u662f Cookie\uff1f<\/h2>\n\n\n\n<p>Cookie \u662f\u5b58\u50a8\u5728\u7528\u6237\u6d4f\u89c8\u5668\u4e2d\u7684\u5c0f\u578b\u6587\u672c\u6570\u636e\uff0c\u901a\u5e38\u7528\u4e8e\u4fdd\u5b58\u7528\u6237\u4f1a\u8bdd\u4fe1\u606f\u3001\u504f\u597d\u8bbe\u7f6e\u3001\u8ddf\u8e2a\u7528\u6237\u884c\u4e3a\u7b49\u3002PHP \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 <code>setcookie()<\/code> \u51fd\u6570\u6765\u64cd\u4f5c Cookie\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u8bbe\u7f6e Cookie<\/h2>\n\n\n\n<p>PHP \u4f7f\u7528 <code>setcookie()<\/code> \u51fd\u6570\u6765\u521b\u5efa Cookie\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>setcookie(name, value, expire, path, domain, secure, httponly);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u6570\u8bf4\u660e\uff1a<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u53c2\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>name<\/code><\/td><td>Cookie \u540d\u79f0<\/td><\/tr><tr><td><code>value<\/code><\/td><td>Cookie \u503c<\/td><\/tr><tr><td><code>expire<\/code><\/td><td>\u8fc7\u671f\u65f6\u95f4\uff08Unix \u65f6\u95f4\u6233\uff0c<code>time() + \u79d2\u6570<\/code>\uff09<\/td><\/tr><tr><td><code>path<\/code><\/td><td>\u9002\u7528\u8def\u5f84\uff08<code>\/<\/code> \u8868\u793a\u6574\u4e2a\u7f51\u7ad9\uff09<\/td><\/tr><tr><td><code>domain<\/code><\/td><td>\u9002\u7528\u57df\u540d\uff08<code>.example.com<\/code> \u9002\u7528\u4e8e\u6240\u6709\u5b50\u57df\uff09<\/td><\/tr><tr><td><code>secure<\/code><\/td><td><code>true<\/code> \u4ec5\u5728 HTTPS \u8fde\u63a5\u4e0b\u4f20\u8f93<\/td><\/tr><tr><td><code>httponly<\/code><\/td><td><code>true<\/code> \u9632\u6b62 JavaScript \u8bbf\u95ee\uff08\u9632\u8303 XSS \u653b\u51fb\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u8bbe\u7f6e\u4e00\u4e2a 1 \u5c0f\u65f6\u540e\u8fc7\u671f\u7684 Cookie<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>setcookie(\"user\", \"JohnDoe\", time() + 3600, \"\/\");<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u8bfb\u53d6 Cookie<\/h2>\n\n\n\n<p>PHP \u901a\u8fc7 <code>$_COOKIE<\/code> \u8d85\u5168\u5c40\u6570\u7ec4\u8bbf\u95ee Cookie\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (isset($_COOKIE&#91;'user'])) {\n    echo \"Hello, \" . $_COOKIE&#91;'user'];\n} else {\n    echo \"Cookie \u672a\u8bbe\u7f6e\";\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u5220\u9664 Cookie<\/h2>\n\n\n\n<p>\u8981\u5220\u9664 Cookie\uff0c\u53ef\u4ee5\u5c06 <code>setcookie()<\/code> \u7684 <code>expire<\/code> \u53c2\u6570\u8bbe\u4e3a\u8fc7\u53bb\u7684\u65f6\u95f4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>setcookie(\"user\", \"\", time() - 3600, \"\/\");<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Cookie \u5b89\u5168\u6027<\/h2>\n\n\n\n<p>\u4e3a\u4e86\u63d0\u9ad8 Cookie \u7684\u5b89\u5168\u6027\uff0c\u5efa\u8bae\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>secure<\/code> \u53c2\u6570<\/strong>\uff1a\u5728 HTTPS \u7ad9\u70b9\u4e0a\u8bbe\u7f6e <code>setcookie()<\/code> \u7684 <code>secure=true<\/code>\uff0c\u9632\u6b62\u660e\u6587\u4f20\u8f93\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   setcookie(\"user\", \"JohnDoe\", time() + 3600, \"\/\", \"\", true, true);<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>httponly<\/code> \u53c2\u6570<\/strong>\uff1a\u9632\u6b62 JavaScript \u8bbf\u95ee\uff0c\u9632\u8303 XSS \u653b\u51fb\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   setcookie(\"user\", \"JohnDoe\", time() + 3600, \"\/\", \"\", false, true);<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>SameSite<\/code> \u4fdd\u62a4<\/strong>\uff08PHP 7.3+ \u652f\u6301\uff09\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   setcookie(\"user\", \"JohnDoe\", &#91;\n       'expires' =&gt; time() + 3600,\n       'path' =&gt; '\/',\n       'secure' =&gt; true,\n       'httponly' =&gt; true,\n       'samesite' =&gt; 'Strict' \/\/ 'Lax' \u6216 'Strict'\n   ]);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. Cookie \u4e0e Session \u7684\u533a\u522b<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5bf9\u6bd4\u9879<\/th><th>Cookie<\/th><th>Session<\/th><\/tr><\/thead><tbody><tr><td>\u5b58\u50a8\u4f4d\u7f6e<\/td><td>\u5ba2\u6237\u7aef\u6d4f\u89c8\u5668<\/td><td>\u670d\u52a1\u5668\u7aef<\/td><\/tr><tr><td>\u9002\u7528\u8303\u56f4<\/td><td>\u8de8\u591a\u4e2a\u9875\u9762\u6301\u4e45\u5b58\u50a8<\/td><td>\u4ec5\u9650\u5f53\u524d\u4f1a\u8bdd<\/td><\/tr><tr><td>\u751f\u547d\u5468\u671f<\/td><td>\u53ef\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4<\/td><td>\u9ed8\u8ba4\u4f1a\u8bdd\u5173\u95ed\u540e\u9500\u6bc1<\/td><\/tr><tr><td>\u5b89\u5168\u6027<\/td><td>\u6613\u88ab\u7be1\u6539<\/td><td>\u66f4\u5b89\u5168<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5982\u679c\u6d89\u53ca\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\uff0c\u5efa\u8bae\u4f7f\u7528 <strong>Session<\/strong> \u800c\u975e Cookie\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. \u5e38\u89c1\u95ee\u9898<\/h2>\n\n\n\n<p><strong>Q1: \u4e3a\u4ec0\u4e48 <code>setcookie()<\/code> \u6ca1\u6709\u751f\u6548\uff1f<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>setcookie()<\/code> <strong>\u5fc5\u987b\u5728\u4efb\u4f55\u8f93\u51fa\u4e4b\u524d\u8c03\u7528<\/strong>\uff08\u5305\u62ec HTML \u4ee3\u7801\u6216 <code>echo<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u786e\u4fdd\u672a\u8c03\u7528 <code>session_start()<\/code> \u4e4b\u524d\u8f93\u51fa\u5185\u5bb9\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>Q2: \u5982\u4f55\u67e5\u770b Cookie\uff1f<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u6d4f\u89c8\u5668\u63a7\u5236\u53f0 (<code>F12<\/code> \u2192 <code>Application<\/code> \u2192 <code>Storage<\/code> \u2192 <code>Cookies<\/code>) \u67e5\u770b\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. \u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.php.net\/manual\/en\/function.setcookie.php\">PHP \u5b98\u65b9\u6587\u6863 &#8211; setcookie()<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/en\/reserved.variables.cookies.php\">PHP \u5b98\u65b9\u6587\u6863 &#8211; $_COOKIE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Cookies\">MDN Web Docs &#8211; HTTP Cookies<\/a><\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4ec0\u4e48\u662f Cookie\uff1f Cookie \u662f\u5b58\u50a8\u5728\u7528\u6237\u6d4f\u89c8\u5668\u4e2d\u7684\u5c0f\u578b\u6587\u672c\u6570\u636e\uff0c\u901a\u5e38\u7528\u4e8e\u4fdd\u5b58\u7528\u6237\u4f1a\u8bdd\u4fe1\u606f\u3001\u504f\u597d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3052,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3051"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3051\/revisions"}],"predecessor-version":[{"id":3053,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3051\/revisions\/3053"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3052"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}