{"id":436,"date":"2011-05-18T22:20:35","date_gmt":"2011-05-18T14:20:35","guid":{"rendered":"http:\/\/zduo.me\/blog\/?p=436"},"modified":"2011-05-18T22:20:35","modified_gmt":"2011-05-18T14:20:35","slug":"%e8%ae%a9javascript%e5%92%8cphp%e9%80%9a%e7%94%a8%e5%80%bc%e4%b8%ba%e4%b8%ad%e6%96%87%e7%9a%84cookie","status":"publish","type":"post","link":"http:\/\/zduo.me\/blog\/?p=436","title":{"rendered":"\u8ba9JavaScript\u548cPHP\u901a\u7528\u503c\u4e3a\u4e2d\u6587\u7684cookie"},"content":{"rendered":"<p>\u5f15\u7528\u5730\u5740\uff1ahttp:\/\/vseb.blog.sohu.com\/67136922.html<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u54ea\u91cc\u90fd\u80fd\u627e\u5230\u7684\u64cd\u4f5ccookie\u7684JavaScript\u51fd\u6570\uff1a<\/p>\n<pre lang='php'>\r\n<script>\r\nfunction SetCookie(name, value)\r\n\/\/\u8bbe\u5b9aCookie\u503c\r\n{\r\nvar expdate = new Date();\r\nvar argv = SetCookie.arguments;\r\nvar argc = SetCookie.arguments.length;\r\nvar expires = (argc > 2) ? argv[2] : null;\r\nvar path = (argc > 3) ? argv[3] : null;\r\nvar domain = (argc > 4) ? argv[4] : null;\r\nvar secure = (argc > 5) ? argv[5] : false;\r\nif(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));\r\ndocument.cookie = name + \"=\" + escape (value) +((expires == null) ? \"\" : (\"; expires=\"+ expdate.toGMTString()))\r\n+((path == null) ? \"\" : (\"; path=\" + path)) +((domain == null) ? \"\" : (\"; domain=\" + domain))\r\n+((secure == true) ? \"; secure\" : \"\");\r\n}\r\n \r\nfunction DelCookie(name)\r\n\/\/\u5220\u9664Cookie\r\n{\r\nvar exp = new Date();\r\nexp.setTime (exp.getTime() - 1);\r\nvar cval = GetCookie (name);\r\ndocument.cookie = name + \"=\" + cval + \"; expires=\"+ exp.toGMTString();\r\n}\r\n \r\nfunction GetCookie(name)\r\n\/\/\u83b7\u5f97Cookie\u7684\u539f\u59cb\u503c\r\n{\r\nvar arg = name + \"=\";\r\nvar alen = arg.length;\r\nvar clen = document.cookie.length;\r\nvar i = 0;\r\nwhile (i < clen)\r\n{\r\nvar j = i + alen;\r\nif (document.cookie.substring(i, j) == arg)\r\nreturn GetCookieVal (j);\r\ni = document.cookie.indexOf(\" \", i) + 1;\r\nif (i == 0) break;\r\n}\r\nreturn null;\r\n}\r\n \r\nfunction GetCookieVal(offset)\r\n\/\/\u83b7\u5f97Cookie\u89e3\u7801\u540e\u7684\u503c\r\n{\r\nvar endstr = document.cookie.indexOf (\";\", offset);\r\nif (endstr == -1)\r\nendstr = document.cookie.length;\r\nreturn unescape(document.cookie.substring(offset, endstr));\r\n}\r\n<\/script>\r\n<\/pre>\n<p>\u5f88\u9057\u61be\uff0c\u5982\u679c\u8ba9PHP\u8bbe\u5b9acookie\u7684\u503c\u4e3a\u4e2d\u6587\uff0c\u7136\u540e\u8ba9\u8fd9\u4e2aJavaScript\u8bfb\u53d6\uff0c\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\uff1a<\/p>\n<pre lang='php'>\r\n<?php\r\nsetcookie(\"a\", \"\u4e2d\u6587\");\r\n?>\r\n<script>\r\ndocument.write(GetCookie(\"a\"));\r\n\/\/\u663e\u793a\u00e4\u00b8\u00ad\u00e6?? \r\n<\/script>\r\n\u53cd\u4e4b\uff0c\u5982\u679c\u8ba9JavaScript\u8bbe\u5b9a\u4e86\u4e00\u4e2a\u4e2d\u6587cookie\u503c\uff0c\u7531PHP\u8fdb\u884c\u8bfb\u53d6\uff0c\u4e5f\u4f1a\u51fa\u73b0\u4e71\u7801\uff1a\r\n\r\n<script>\r\nSetCookie(\"b\",\"\u4e2d\u6587\");\r\n<\/script>\r\n<?php\r\necho $_COOKIE[\"b\"];\r\n\/\/\u663e\u793a% u4E2D% u6587\r\n?>\r\n<\/pre>\n<p>\u89c2\u5bdf\u4e0a\u9762\u7684\u4e71\u7801\uff0c\u4f60\u4f1a\u53d1\u73b0% u4E2D% u6587\u521a\u597d\u662f\u201c\u4e2d\u6587\u201d\u4e24\u4e2a\u6c49\u5b57\u7684unicode\u7f16\u7801\uff0c\u7b2c\u4e00\u4e2a\u60f3\u6cd5\u662f\u76f4\u63a5\u89e3\u7801\uff0c\u4f46\u662fPHP\u91cc\u9762\u4f3c\u4e4e\u6ca1\u6709\u53ef\u4ee5\u628aunicode\u7f16\u7801\u8f6c\u6362\u4e3a\u6c49\u5b57\u7684\u4fbf\u6377\u65b9\u6cd5\u3002\uff08WordPress\u5012\u662f\u4e0d\u9519\uff0c\u4e0d\u52a0\u7a7a\u683c\u5012\u662f\u7ed9\u89e3\u7801\u51fa\u6765\u4e86\uff01\uff09<\/p>\n<p>\u5728\u300aJavaScript: The Definitive Guide, 4th Edition\u300b\u4e2d\u5199\u5230\uff1a<\/p>\n<p>In client-side JavaScript, a common use of escape( ) is to encode cookie values, which have restrictions on the punctuation characters they may contain.<br \/>\n\u5728\u5ba2\u6237\u7aef\u811a\u672c\u7a0b\u5e8f\u4e2d\uff0cescape( )\u51fd\u6570\u53ef\u4ee5\u88ab\u7528\u4f5c\u5bf9\u5177\u6709\u4e0d\u89c4\u8303\u6807\u70b9\u7684cookie\u8fdb\u884c\u7f16\u7801\u3002\uff08\u5c31\u50cf\u6211\u4eec\u51fd\u6570\u4e2d\u6240\u7528\u5230\u7684\u4e00\u6837\uff09<\/p>\n<p>Although the escape( ) function was standardized in the first version of ECMAScript, it has been deprecated and removed from the standard by ECMAScript v3. Implementations of ECMAScript are likely to implement this function, but they are not required to. In JavaScript 1.5 and JScript 5.5 and later, you should use encodeURI( ) and encodeURIComponent( ) instead of escape( ).<br \/>\n\u867d\u7136escape( ) \u5df2\u7ecf\u5728ECMAScript\u4e2d\u88ab\u6807\u51c6\u5316\uff0c\u4f46\u662f\u5728ECMAScript v3\u4e2d\uff0cescape( ) \u88ab\u5254\u51fa\uff0c\u5982\u679c\u9700\u8981\u5728JavaScript 1.5 \u548cJScript 5.5\u4ee5\u540e\u7684\u7248\u672c\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u5efa\u8bae\u4f7f\u7528encodeURI( )\u548cencodeURIComponent( )\u3002<\/p>\n<p>\u6309\u7167\u624b\u518c\u7684\u5efa\u8bae\uff0c\u6211\u4fee\u6539\u4e86JavaScript\u51fd\u6570\u4e2d\u7684escape()\u548cunescape()\u4e3aencodeURI()\u548cdecodeURI()\uff0c\u7ed3\u679c\u5f02\u5e38\u7684\u987a\u5229\uff0c\u4e0b\u9762\u7ed9\u51fa\u4fee\u6539\u540e\u7684\u4ee3\u7801\u548c\u8c03\u8bd5\u7a0b\u5e8f\uff1a<\/p>\n<pre lang='php'>\r\n<?php\r\nsetcookie(\"a\", \"\u4e2d\u6587\");\r\n?>\r\n<meta http-equiv=\"content-type\" content=\"text\/html; charset=utf-8\"\/>\r\n<script>\r\nfunction SetCookie(name, value)\r\n\/\/\u8bbe\u5b9aCookie\u503c\r\n{\r\nvar expdate = new Date();\r\nvar argv = SetCookie.arguments;\r\nvar argc = SetCookie.arguments.length;\r\nvar expires = (argc > 2) ? argv[2] : null;\r\nvar path = (argc > 3) ? argv[3] : null;\r\nvar domain = (argc > 4) ? argv[4] : null;\r\nvar secure = (argc > 5) ? argv[5] : false;\r\nif(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));\r\ndocument.cookie = name + \"=\" + encodeURI (value) +((expires == null) ? \"\" : (\"; expires=\"+ expdate.toGMTString()))\r\n+((path == null) ? \"\" : (\"; path=\" + path)) +((domain == null) ? \"\" : (\"; domain=\" + domain))\r\n+((secure == true) ? \"; secure\" : \"\");\r\n}\r\n \r\nfunction DelCookie(name)\r\n\/\/\u5220\u9664Cookie\r\n{\r\nvar exp = new Date();\r\nexp.setTime (exp.getTime() - 1);\r\nvar cval = GetCookie (name);\r\ndocument.cookie = name + \"=\" + cval + \"; expires=\"+ exp.toGMTString();\r\n}\r\n \r\nfunction GetCookie(name)\r\n\/\/\u83b7\u5f97Cookie\u7684\u539f\u59cb\u503c\r\n{\r\nvar arg = name + \"=\";\r\nvar alen = arg.length;\r\nvar clen = document.cookie.length;\r\nvar i = 0;\r\nwhile (i < clen)\r\n{\r\nvar j = i + alen;\r\nif (document.cookie.substring(i, j) == arg)\r\nreturn GetCookieVal (j);\r\ni = document.cookie.indexOf(\" \", i) + 1;\r\nif (i == 0) break;\r\n}\r\nreturn null;\r\n}\r\n \r\nfunction GetCookieVal(offset)\r\n\/\/\u83b7\u5f97Cookie\u89e3\u7801\u540e\u7684\u503c\r\n{\r\nvar endstr = document.cookie.indexOf (\";\", offset);\r\nif (endstr == -1)\r\nendstr = document.cookie.length;\r\nreturn decodeURI(document.cookie.substring(offset, endstr));\r\n}\r\n<\/script>\r\n<script>\r\ndocument.write(GetCookie(\"a\"));\r\n\/\/\u663e\u793a\u201c\u4e2d\u6587\u201d\r\n<\/script>\r\n<script>\r\nSetCookie(\"b\",\"\u4e2d\u6587\");\r\n<\/script>\r\n<?php\r\necho $_COOKIE[\"b\"];\r\n\/\/\u663e\u793a\u201c\u4e2d\u6587\u201d\r\n?>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u7528\u5730\u5740\uff1ahttp:\/\/vseb.blog.sohu.com\/67136922.html \u8fd9\u662f\u4e00\u4e2a\u54ea\u91cc\u90fd\u80fd\u627e\u5230\u7684\u64cd\u4f5ccookie\u7684JavaScript\u51fd\u6570\uff1a \u5f88\u9057\u61be\uff0c\u5982\u679c\u8ba9PHP\u8bbe\u5b9acookie\u7684\u503c\u4e3a\u4e2d\u6587\uff0c\u7136\u540e\u8ba9\u8fd9\u4e2aJavaScript\u8bfb\u53d6\uff0c\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\uff1a \u53cd\u4e4b\uff0c\u5982\u679c\u8ba9JavaScript\u8bbe\u5b9a\u4e86\u4e00\u4e2a\u4e2d\u6587cookie\u503c\uff0c\u7531PHP\u8fdb\u884c\u8bfb\u53d6\uff0c\u4e5f\u4f1a\u51fa\u73b0\u4e71\u7801\uff1a \u89c2\u5bdf\u4e0a\u9762\u7684\u4e71\u7801\uff0c\u4f60\u4f1a\u53d1\u73b0% u4E2D% u6587\u521a\u597d\u662f\u201c\u4e2d\u6587\u201d\u4e24\u4e2a\u6c49\u5b57\u7684unicode\u7f16\u7801\uff0c\u7b2c\u4e00\u4e2a\u60f3\u6cd5\u662f\u76f4\u63a5\u89e3\u7801\uff0c\u4f46\u662fPHP\u91cc\u9762\u4f3c\u4e4e\u6ca1\u6709\u53ef\u4ee5\u628aunicode\u7f16\u7801\u8f6c\u6362\u4e3a\u6c49\u5b57\u7684\u4fbf\u6377\u65b9\u6cd5\u3002\uff08WordPress\u5012\u662f\u4e0d\u9519\uff0c\u4e0d\u52a0\u7a7a\u683c\u5012\u662f\u7ed9\u89e3\u7801\u51fa\u6765\u4e86\uff01\uff09 \u5728\u300aJavaScript: The Definitive Guide, 4th Edition\u300b\u4e2d\u5199\u5230\uff1a In client-side JavaScript, a common use of escape( ) is to encode cookie values, which have restrictions on the punctuation characters they may contain. \u5728\u5ba2\u6237\u7aef\u811a\u672c\u7a0b\u5e8f\u4e2d\uff0cescape( )\u51fd\u6570\u53ef\u4ee5\u88ab\u7528\u4f5c\u5bf9\u5177\u6709\u4e0d\u89c4\u8303\u6807\u70b9\u7684cookie\u8fdb\u884c\u7f16\u7801\u3002\uff08\u5c31\u50cf\u6211\u4eec\u51fd\u6570\u4e2d\u6240\u7528\u5230\u7684\u4e00\u6837\uff09 Although &hellip; <a href=\"http:\/\/zduo.me\/blog\/?p=436\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,8],"tags":[34,15,166,51],"_links":{"self":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/436"}],"collection":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=436"}],"version-history":[{"count":4,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/436\/revisions"}],"predecessor-version":[{"id":440,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/436\/revisions\/440"}],"wp:attachment":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=436"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}