{"id":577,"date":"2010-12-18T10:00:16","date_gmt":"2010-12-18T10:00:16","guid":{"rendered":""},"modified":"2010-12-18T10:00:16","modified_gmt":"2010-12-18T10:00:16","slug":"577","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=577","title":{"rendered":"PHP\u7684\u52a0\u5bc6\u89e3\u5bc6\u5185\u90e8\u7b97\u6cd5"},"content":{"rendered":"<p>\u6700\u8fd1\u53d1\u73b0\u5728URL\u8df3\u8f6c\u7684\u65f6\u5019\u7684\u4e09\u4e2a\u8d85\u597d\u7528\u7684PHP\u52a0\u5bc6\u89e3\u5bc6\u51fd\u6570,\u8c8c\u4f3c\u662fdiscuz\u91cc\u7684\u2026<br \/>\u4f7f\u7528\u8fd9\u4e9b\u52a0\u5bc6\u89e3\u5bc6\u7684\u539f\u56e0\u662f\u56e0\u4e3a\u6709\u65f6\u81ea\u5df1\u7684URL\u5730\u5740\u88ab\u4eba\u83b7\u53d6\u4ee5\u540e\u60f3\u7834\u89e3\u4f60\u91cc\u9762\u4f20\u503c\u7684\u5185\u5bb9\u5c31\u5fc5\u987b\u77e5\u9053\u4f60\u7684key\uff0c\u6ca1\u6709key\uff0c\u4ed6\u5e94\u8be5\u8981\u7834\u4e86\u4e00\u9635\u5b50\u624d\u80fd\u77e5\u9053\u4f60URL\u91cc\u9762\u7684\u5185\u5bb9\u5427&#8230;<br \/>\u95f2\u8bdd\u5c11\u8bf4,\u5148\u5c06\u5b83\u4eec\u6253\u5305\u6210\u4e00\u4e2a\u6587\u4ef6\u5c31\u53ebfun.php\u5427<br \/><code><br \/>&lt;?php<br \/>function passport_encrypt($txt, $key) {<br \/>srand((double)microtime() * 1000000);<br \/>$encrypt_key = md5(rand(0, 32000));<br \/>$ctr = 0;<br \/>$tmp = '';<br \/>for($i = 0;$i &lt; strlen($txt); $i++) {<br \/>&nbsp;&nbsp; $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;<br \/>&nbsp;&nbsp; $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);<br \/>}<br \/>return base64_encode(passport_key($tmp, $key));<br \/>}<\/p>\n<p>function passport_decrypt($txt, $key) {<br \/>$txt = passport_key(base64_decode($txt), $key);<br \/>$tmp = '';<br \/>for($i = 0;$i &lt; strlen($txt); $i++) {<br \/>&nbsp;&nbsp; $md5 = $txt[$i];<br \/>&nbsp;&nbsp; $tmp .= $txt[++$i] ^ $md5;<br \/>}<br \/>return $tmp;<br \/>}<\/p>\n<p>function passport_key($txt, $encrypt_key) {<br \/>$encrypt_key = md5($encrypt_key);<br \/>$ctr = 0;<br \/>$tmp = '';<br \/>for($i = 0; $i &lt; strlen($txt); $i++) {<br \/>&nbsp;&nbsp; $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;<br \/>&nbsp;&nbsp; $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];<br \/>}<br \/>return $tmp;<br \/>}<br \/>?&gt;<br \/><\/code><br \/>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u2026\u52a0\u6df1\u5bf9\u8fd9\u4e09\u4e2a\u52a0\u5bc6\u89e3\u5bc6\u51fd\u6570\u7684\u7406\u89e3\u2026<br \/>\/\/string.php<br \/><code><br \/>&lt;?php<br \/>include \u201cfun.php\u201d;<\/p>\n<p>$txt = \u201cThis is a test\u201d;<br \/>$key = \u201ctestkey\u201d;<br \/>$encrypt = passport_encrypt($txt,$key);<br \/>$decrypt = passport_decrypt($encrypt,$key);<\/p>\n<p>echo $txt.\u201d&lt;br&gt;&lt;hr&gt;\u201d;<br \/>echo $encrypt.\u201d&lt;br&gt;&lt;hr&gt;\u201d;<br \/>echo $decrypt.\u201d&lt;br&gt;&lt;hr&gt;\u201d;<br \/>?&gt;<br \/><\/code><br \/>\/\/array.php<br \/><code><br \/>&lt;?php<br \/>include \u201cfun.php\u201d;<\/p>\n<p>$array = array(<br \/>\"a\" =&gt; \"1\",<br \/>\"b\" =&gt; \"2\",<br \/>\"c\" =&gt; \"3\",<br \/>\"d\" =&gt; \"4\"<br \/>);<br \/>\/\/serialize\u4ea7\u751f\u4e00\u4e2a\u53ef\u5b58\u50a8\u7684\u503c,\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32,unserialize\u8fd8\u539f<br \/>$txt = serialize($array);<br \/>$key = \u201ctestkey\u201d;<br \/>$encrypt = passport_encrypt($txt,$key);<br \/>$decrypt = passport_decrypt($encrypt,$key);<br \/>$decryptArray = unserialize($decrypt);<\/p>\n<p>echo $txt.\u201d&lt;br&gt;&lt;hr&gt;\u201d;<br \/>echo $encrypt.\u201d&lt;br&gt;&lt;hr&gt;\u201d;<br \/>echo $decrypt.\u201d&lt;br&gt;&lt;hr&gt;\u201d;<br \/>echo $decryptArray.\u201d&lt;br&gt;&lt;hr&gt;\u201d;<br \/>?&gt;<br \/><\/code><br \/>\u5173\u952e\u7684\u5730\u65b9\u6765\u4e86\u2026\u5f53\u4f60\u8981\u8df3\u8f6c\u5230\u53e6\u5916\u4e00\u4e2a\u7f51\u5740,\u4f46\u53c8\u8981\u4fdd\u8bc1\u4f60\u7684session\u65e0\u8bef\u7684\u65f6\u5019,\u4f60\u9700\u8981\u5bf9session\u4f5c\u4e00\u4e2a\u5904\u7406.\u8c8c\u4f3c\u4e00\u4e2a\u516c\u53f8\u6709\u4e00\u4e2a\u7f51\u7ad9\u53c8\u6709\u4e00\u4e2a\u8bba\u575b,\u4e24\u4e2a\u5730\u65b9\u90fd\u6709\u6ce8\u518c\u548c\u767b\u5f55,\u4f46\u53c8\u4e0d\u60f3\u8ba9\u7528\u6237\u5728\u4e3b\u9875\u767b\u5f55\u540e\u8df3\u8f6c\u5230\u8bba\u575b\u7684\u65f6\u5019session\u5931\u6548,\u5373\u662f\u767b\u5f55\u4e00\u6b21\u8dd1\u5b8c\u6574\u95f4\u516c\u53f8\u2026<br \/>\u90a3\u8981\u600e\u6837\u6765\u5904\u7406\u7528\u6237\u7684session\u5462\u2026<br \/>\u7f51\u9875\u90fd\u662f\u65e0\u72b6\u6001\u7684,\u5982\u679c\u8981\u5728\u65b0\u7684\u7f51\u9875\u4e2d\u7ee7\u7eed\u4f7f\u7528session,\u5219\u9700\u8981\u628asession\u4ece\u4e00\u4e2a\u5730\u65b9\u79fb\u5230\u53e6\u4e00\u4e2a\u5730\u65b9,\u53ef\u80fd\u6709\u4e9b\u4eba\u5df2\u7ecf\u60f3\u5230\u4e86,\u6211\u53ef\u4ee5\u901a\u8fc7url\u4f20\u5740\u7684\u65b9\u5f0f\u6765\u8c03\u7528\u5b83\u2026.\u800cPHP\u6709\u4e2a\u5904\u7406session\u7684\u53d8\u91cf,\u53eb$_SESSION.\u4e8e\u662f\u2026.<br \/>\u5c06\u9700\u8981\u6ce8\u518c\u7684session\u8f6c\u6362\u6210\u4e00\u4e2a\u6570\u7ec4\u5427.\u90a3\u4e48,\u4f60\u53ef\u4ee5\u8fd9\u6837\u5199:<br \/>\/\/login.php<br \/><code><br \/>&lt;?php<br \/>session_start();<br \/>include \u201cfun.php\u201d;<br \/>\u2026.<br \/>$_SESSION[\u201cuserid\u201d];<br \/>$_SESSION[\u201cusername\u201d];<br \/>$_SESSION[\u201cuserpwd\u201d];<br \/>\u2026<br \/>header(\"Location: <a href=\"http:\/\/\" target=\"_blank\" rel=\"external\">http:\/\/<\/a>$domain\/process.php?s=\".urlencode(passport_encrypt(serialize($_SESSION),\"sessionkey\")));<br \/>?&gt;<br \/><\/code><br \/>\u4e0a\u4f8b\u4e2d\u5148\u7528serialize\u5c06$_SESSION\u53d8\u6210\u53ef\u5b58\u50a8\u7684\u6570\u636e,\u7136\u540e\u901a\u8fc7passport_encrypt\u5c06\u8fd9\u4e2a\u6570\u636e\u52a0\u5bc6,\u52a0urlencode\u7684\u539f\u56e0\u662f\u56e0\u4e3a$_SESSION\u52a0\u5bc6\u65f6,\u6709\u53ef\u80fd\u4f1a\u4ea7\u751f\u50cf\u6599\u60f3\u4e0d\u5230\u7684\u7f16\u7801,\u6240\u4ee5\u4ee5\u9632\u4e07\u4e00\u2026(\u4e8b\u5b9e\u8bc1\u660e\u975e\u5e38\u6709\u6548)<br \/>\u5904\u7406\u4e0b\u5148<br \/>\/\/process.php<br \/><code><br \/>&lt;?php<br \/>session_start();<br \/>include \u201cfun.php\u201d;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;$_SESSION=unserialize(passport_decrypt($_GET[\"s\"],\"sessionkey\"));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;header(\"Location: <a href=\"http:\/\/\" target=\"_blank\" rel=\"external\">http:\/\/<\/a>$domain\/index.php\");<br \/>?&gt;<br \/><\/code><br \/>\u5148\u7528$_GET[\u201cs\u201d]\u83b7\u53d6URL\u7684\u53c2\u6570,\u7136\u540e\u7528passport_decrypt\u5c06\u5176\u89e3\u5bc6,\u518d\u7528unserialize\u5c06\u5176\u6570\u636e\u8fd8\u539f\u6210\u539f\u59cb\u6570\u636e<br \/>\u5230\u4e86\u8fd9\u6b65\u5904\u7406,\u4f60\u7684\u7f51\u9875\u5c31\u53ef\u80fd\u901a\u8fc7header\u81ea\u7531\u8df3\u8f6c\u5566\u2026.<br \/>\u8fd9\u79cd\u65b9\u6cd5\u8fd8\u6d89\u53ca\u5230\u5b89\u5168\u6027\u7684\u95ee\u9898,\u5982\u679c\u4f60\u7684url\u5730\u5740\u5728\u4f20\u5740\u7684\u8fc7\u7a0b\u4e2d\u88ab\u4eba\u5bb6\u83b7\u53d6\u7684\u8bdd,\u90a3\u5c31\u771f\u7684\u662f\u4e0d\u597d\u610f\u601d\u4e86\u2026\u4eba\u5bb6\u867d\u7136\u53ef\u80fd\u7834\u89e3\u4e0d\u4e86url\u91cc\u8fb9\u7684\u5185\u5bb9,\u4f46\u4eba\u5bb6\u4e5f\u53ef\u4ee5\u76f4\u63a5\u7528\u8fd9\u4e2aurl\u5730\u5740\u6765\u767b\u5f55\u4f60\u7684\u4e00\u4e9b\u4e2a\u4eba\u8d26\u6237\u554a,\u90ae\u7bb1\u5e10\u6237\u554a\u751a\u81f3\u94f6\u884c\u5e10\u6237(\u5f53\u7136\u5f88\u5c11\u4eba\u4f1a\u8fd9\u6837\u5199,\u6211\u4f8b\u5916,\u54c8\u54c8)\u2026\u542c\u8d77\u6765\u597d\u6015\u2026.\u4f46\u5176\u5b9e\u4f60\u53ef\u4ee5\u5728\u8df3\u8f6c\u9875\u9762\u4f5c\u53d6\u6d88session\u5904\u7406\u2026.<br \/>\u4ee5\u4e0b\u662f\u52a0\u5f3a\u7248\u7684process.php<br \/><code><br \/>&lt;?php<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session_start();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include_once \"fun.php\";<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$_SESSION=unserialize(passport_decrypt($_GET[\"s\"],\"sessionkey\"));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((time()-$_SESSION[\"TIME\"])&gt;30){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header(\"Location: <a href=\"http:\/\/\" target=\"_blank\" rel=\"external\">http:\/\/<\/a>$domain\/ login.php\");<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset($_SESSION[\"USERNAME\"]);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset($_SESSION[\"PASSWORD\"]);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header(\"Location: <a href=\"http:\/\/\" target=\"_blank\" rel=\"external\">http:\/\/<\/a>$domain\/ index.php\");<br \/>?&gt;<br \/><\/code><br \/>\u5199\u8fd9\u4e2a\u6587\u4ef6\u4e4b\u524d,\u4f60\u8fd8\u8981\u5728\u767b\u5f55\u90a3\u8fb9\u8bbe\u7f6e<br \/>$_SESSION[&#8220;TIME&#8221;] = time();<br \/>\u8bbe\u7f6e\u8fd9\u4e2a\u7684\u539f\u56e0\u4e3b\u8981\u662f\u83b7\u53d6\u4e24\u8fb9\u7684\u65f6\u95f4,\u5982\u679c\u8df3\u8f6c\u7684\u65f6\u5019\u8d85\u8fc730\u79d2\u7684\u65f6\u5019,\u4f60\u5c31\u53ef\u4ee5\u8ba9\u5b83\u8df3\u8f6c\u5230login.php\u767b\u5f55\u9875\u9762,\u7f51\u901f\u6162\u7684\u5ba2\u6237\u5c31\u4e0d\u597d\u610f\u601d\u5566\u2026\u4f46\u8fd9\u4e5f\u9884\u9632\u4e86\u5982\u679c\u6b64url\u88ab\u4eba\u83b7\u53d6,\u800c\u8fd9\u4e2a\u4eba\u53c8\u6ca1\u6709\u572830\u79d2\u5185\u767b\u5f55\u7684\u8bdd,\u90a3\u5c31\u4e0d\u597d\u610f\u601d\u554a,\u8d85\u65f6\u91cd\u65b0\u767b\u5f55.<br \/>$_SESSION[&#8220;USERNAME&#8221;]\u548c$_SESSION[&#8220;PASSWORD&#8221;] \u8fd9\u4e24\u4e2a\u4e1c\u4e1c\u5c31\u662f\u7528\u6237\u767b\u5f55\u65f6\u9700\u8981\u8f93\u5165\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u4e86\u2026.\u53d6\u6d88\u8fd9\u4e24\u4e2asession\u7684\u539f\u56e0\u5c31\u662f\u56e0\u4e3a\u5982\u679c\u4f60\u7684url\u88ab\u4eba\u83b7\u53d6\u4e86,\u90a3\u4e2a\u4eba\u867d\u7136\u5728\u8d85\u8fc730\u79d2\u5185\u8df3\u8f6c\u5230loign.php\u7684\u9875\u9762,\u4f46\u90a3\u4e9b\u4f20\u8fc7\u6765\u7684session\u4f9d\u7136\u6709\u6548,\u53ea\u8981\u5c06url\u540e\u7f00login.php\u6539\u4e3aindex.php\u2026.\u90a3\u4ed6\u4e00\u6837\u767b\u5f55\u6210\u529f\u2026<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u53d1\u73b0\u5728URL\u8df3\u8f6c\u7684\u65f6\u5019\u7684\u4e09\u4e2a\u8d85\u597d\u7528\u7684PHP\u52a0\u5bc6\u89e3\u5bc6\u51fd\u6570,\u8c8c\u4f3c\u662fd&#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-577","post","type-post","status-publish","format-standard","hentry","category-Php"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/577","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=577"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/577\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}