{"id":521,"date":"2010-09-26T09:08:35","date_gmt":"2010-09-26T09:08:35","guid":{"rendered":""},"modified":"2010-09-26T09:08:35","modified_gmt":"2010-09-26T09:08:35","slug":"521","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=521","title":{"rendered":"PHP\u4e4bCOOKIE\u652f\u6301\u8be6\u89e3"},"content":{"rendered":"<p>\u5efa\u7acb\u5546\u4e1a\u7ad9\u70b9\u6216\u8005\u529f\u80fd\u6bd4\u8f83\u5b8c\u5584\u7684\u4e2a\u4eba\u7ad9\u70b9\uff0c\u5e38\u5e38\u9700\u8981\u8bb0\u5f55\u8bbf\u95ee\u8005\u7684\u4fe1\u606f\uff0c\u5728PHP\u4e2d\u63d0\u4f9b\u4e86\u4e24\u79cd\u65b9\u4fbf\u7684\u624b\u6bb5\uff1asession\u548ccookie\u529f\u80fd.\u4e3a\u4e86\u6c38\u4e45\u7684\u4fdd\u6301\u7528\u6237\u4fe1\u606f,\u5219cookie\u662f\u6700\u4fbf\u6377\u7684\u624b\u6bb5.\u5728\u8fd9\u91cc\u6211\u5c06\u4e3a\u5927\u5bb6\u8be6\u7ec6\u7684\u8bb2\u89e3cookie\u7684\u529f\u80fd\u53ca\u4f7f\u7528\u65b9\u6cd5. <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u4e00: \u8bbe\u7f6ecookie <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u4f7f\u7528cookie\u524d\u5fc5\u987b\u8bbe\u7f6ecookie. <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u51fd\u6570\u539f\u578b:int setcookie(string name,string value,int expire,string path,string domain,int secure) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u5176\u4e2d,\u9664name\u5916,\u6240\u6709\u7684\u53c2\u6570\u90fd\u662f\u53ef\u9009\u7684,\u53ef\u4ee5\u7528\u7a7a\u7684\u5b57\u7b26\u4e32\u8868\u793a\u672a\u8bbe\u7f6e. <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u5c5e\u6027value: \u7528\u6765\u6307\u5b9a\u503c. <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u5c5e\u6027path:&nbsp;&nbsp;\u7528\u6765\u6307\u5b9acookie\u88ab\u53d1\u9001\u5230\u670d\u52a1\u5668\u7684\u54ea\u4e00\u4e2a\u76ee\u5f55\u8def\u5f84\u4e0b. <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u5c5e\u6027domain:\u80fd\u591f\u5728\u6d4f\u89c8\u5668\u7aef\u5bf9cookie\u7684\u53d1\u9001\u8fdb\u884c\u9650\u5b9a.&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expire\u53c2\u6570:\u7528\u6765\u6307\u5b9acookie\u7684\u6709\u6548\u65f6\u95f4,\u5b83\u662f\u4e00\u4e2a\u6807\u51c6\u7684Unix\u65f6\u95f4\u6807\u8bb0. <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u53ef\u4ee5\u7528time()\u6216\u8005mktime()\u51fd\u6570\u53d6\u5f97,\u4ee5\u79d2\u4e3a\u5355\u4f4d. <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secure\u53c2\u6570:\u8868\u793a\u8fd9\u4e2acookie\u662f\u5426\u901a\u8fc7\u52a0\u5bc6\u7684HTTPS\u534f\u8bae\u5728\u7f51\u7edc\u4e0a\u4f20\u8f93. <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u4e8c: \u8bbe\u7f6ecookie\u65f6\u7684\u6ce8\u610f\u4e8b\u9879 <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u5728\u540c\u4e00\u4e2a\u9875\u9762\u4e2d\u8bbe\u7f6ecookie,\u5b9e\u9645\u4e0a\u662f\u6309\u4ece\u540e\u5f80\u524d\u7684\u987a\u5e8f\u8fdb\u884c\u7684.\u5982\u679c\u8981\u5148\u5220\u9664\u4e00\u4e2acookie,\u518d\u5199\u5165\u4e00\u4e2acookie,\u5219\u5fc5\u987b\u5148\u5199\u5199\u5165\u8bed\u53e5,\u518d\u5199\u5220\u9664\u8bed\u53e5.\u5426\u5219\u4f1a\u51fa\u73b0\u9519\u8bef. <br \/>&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\u4e09: setcookie\u4e3e\u4f8b <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u7b80\u5355\u7684: setcookie(&#8220;mycookie&#8221;,&#8221;value_of_mycookie&#8221;); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u5e26\u5931\u6548\u65f6\u95f4\u7684: setcookie(&#8220;withExpire&#8221;,&#8221;Expire_in_1_hour&#8221;,time()+3600); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u4ec0\u4e48\u90fd\u6709\u7684:setcookie(&#8220;FullCookie&#8221;,&#8221;Full_cookie_value&#8221;,time+3600,&#8221;\/forum&#8221;,&#8221;www.123.com&#8221;,1); <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u56db: cookie\u7684\u4e00\u4e9b\u7279\u70b9 <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cookie\u662f\u9762\u5411\u8def\u5f84\u7684.\u7f3a\u7701path\u5c5e\u6027\u65f6,WEB\u670d\u52a1\u5668\u9875\u4f1a\u81ea\u52a8\u4f20\u9012\u5f53\u524d\u8def\u5f84\u7ed9\u6d4f\u89c8\u5668.\u6307\u5b9a\u8def\u5f84\u4f1a\u5f3a\u5236\u670d\u52a1\u5668\u4f7f\u7528\u8bbe\u7f6e\u7684\u8def\u5f84. <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u5728\u4e00\u4e2a\u76ee\u5f55\u9875\u9762\u91cc\u8bbe\u7684cookie\u5728\u53e6\u4e00\u4e2a\u76ee\u5f55\u7684\u9875\u9762\u91cc\u662f\u770b\u4e0d\u5230\u7684. <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u4e94: \u63a5\u6536\u548c\u5904\u7406cookie <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PHP\u5bf9cookie\u7684\u5904\u7406\u662f\u5168\u81ea\u52a8\u7684,\u548c\u5904\u7406FORM\u53d8\u91cf\u7684\u539f\u5219\u4e00\u6837.\u5f53\u7136\u4e5f\u53ef\u4ee5\u4f7f\u7528PHP\u5168\u5c40\u53d8\u91cf,$HTTP_COOKIE_VARS\u6570\u7ec4. <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u4f8b:&nbsp;&nbsp;echo $mycookie; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $cookie Array[0]; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo count($cookie Array); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $HTTP_COOKIE_VARS[&#8220;mycookie&#8221;]; <br \/>&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;\u516d: \u5220\u9664cookie <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(1)\u8c03\u7528\u53ea\u5e26\u6709name\u53c2\u6570\u7684setcookie(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(2)\u4f7f\u5931\u6548\u65f6\u95f4\u4e3atime()\u6216time-1;&nbsp;&nbsp;<br \/>&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;\u4e03: \u4f7f\u7528cookie\u7684\u9650\u5236 <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(1) \u5fc5\u987b\u5728HTML\u6587\u4ef6\u7684\u5185\u5bb9\u8f93\u51fa\u4e4b\u524d\u8bbe\u7f6e; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(2)\u4e0d\u540c\u7684\u6d4f\u89c8\u5668\u5bf9cookie\u7684\u5904\u7406\u4e0d\u4e00\u81f4,\u4f7f\u7528\u65f6\u4e00\u5b9a\u8981\u8003\u8651; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(3)\u5ba2\u6237\u7aef\u7684\u9650\u5236,\u6bd4\u5982\u7528\u6237\u8bbe\u7f6e\u7981\u6b62cookie,\u5219cookie\u4e0d\u80fd\u5efa\u7acb; <br \/>&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;\u516b: \u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50,\u5e0c\u671b\u5927\u5bb6\u5bf9cookie\u6709\u66f4\u52a0\u6df1\u523b\u7684\u8ba4\u8bc6 <\/p>\n<p><code><br \/>&lt;? <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/cookie.php <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!isset($flag)) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setcookie(\"mycookie\",\"this my cookie!\"); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header(\"location:cookie.php?flag=1\"); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ?&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;html&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;body&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;? <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"cookie\u4e2d\u6709:\".$mycookie; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ?&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/body&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/html&gt; <br \/><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5efa\u7acb\u5546\u4e1a\u7ad9\u70b9\u6216\u8005\u529f\u80fd\u6bd4\u8f83\u5b8c\u5584\u7684\u4e2a\u4eba\u7ad9\u70b9\uff0c\u5e38\u5e38\u9700\u8981\u8bb0\u5f55\u8bbf\u95ee\u8005\u7684\u4fe1\u606f\uff0c\u5728&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-521","post","type-post","status-publish","format-standard","hentry","category-Php"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=521"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/521\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}