{"id":208,"date":"2010-09-21T11:01:10","date_gmt":"2010-09-21T03:01:10","guid":{"rendered":"http:\/\/zduo.me\/blog\/?p=208"},"modified":"2010-09-21T11:03:16","modified_gmt":"2010-09-21T03:03:16","slug":"discuz-%e7%94%a8%e6%88%b7%e4%bf%a1%e6%81%af%e5%8a%a0%e5%af%86%e7%ae%97%e6%b3%95-authcode%e5%8a%a0%e5%af%86%e8%a7%a3%e5%af%86%e5%87%bd%e6%95%b0%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"http:\/\/zduo.me\/blog\/?p=208","title":{"rendered":"Discuz \u7528\u6237\u4fe1\u606f\u52a0\u5bc6\u7b97\u6cd5-authcode\u52a0\u5bc6\/\u89e3\u5bc6\u51fd\u6570\u8be6\u89e3"},"content":{"rendered":"<p>\u5f15\u7528\u5730\u5740\uff1ahttp:\/\/bbs.itokit.com\/thread-4009-1-1.html<\/p>\n<pre lang='php'>\r\n\/**\r\n*\u7528\u6237\u4fe1\u606f\u52a0\u5bc6\u89e3\u5bc6\u51fd\u6570\r\n*\r\n*\u5f85\u52a0\u5bc6\u5185\u5bb9\u7528\\t\u5206\u5272\r\n*@return String \u52a0\u5bc6\u6216\u89e3\u5bc6\u5b57\u7b26\u4e32\r\n*@param String $string \u5f85\u52a0\u5bc6\u6216\u89e3\u5bc6\u5b57\u7b26\u4e32\r\n*@param String $operation \u64cd\u4f5c\u7c7b\u578b\u5b9a\u4e49 DECODE=\u89e3\u5bc6 ENDODE=\u52a0\u5bc6\r\n*@param String $key \u52a0\u5bc6\u7b97\u5b50\r\n*\/\r\nfunction authcode($string, $operation, $key = '') {\r\n\/**\r\n*\u83b7\u53d6\u5bc6\u7801\u7b97\u5b50,\u5982\u672a\u6307\u5b9a\uff0c\u91c7\u53d6\u7cfb\u7edf\u9ed8\u8ba4\u7b97\u5b50\r\n*\u9ed8\u8ba4\u7b97\u5b50\u662f\u8bba\u575b\u6388\u6743\u7801\u548c\u7528\u6237\u6d4f\u89c8\u5668\u4fe1\u606f\u7684md5\u6563\u5217\u503c\r\n*$GLOBALS['discuz_auth_key']----\u5168\u5c40\u53d8\u91cf\r\n*\u53d6\u503c\u4e3a:md5($_DCACHE['settings']['authkey'].$_SERVER['HTTP_USER_AGENT'])\r\n*$_DCACHE['settings']['authkey']\u662f\u8bba\u575b\u5b89\u88c5\u65f6\u751f\u6210\u768415\u4f4d\u968f\u673a\u5b57\u7b26\u4e32\r\n*\/\r\n    $key = md5($key ? $key : $GLOBALS['discuz_auth_key']);\r\n    $key_length = strlen($key);\r\n\/**\r\n*\u5982\u679c\u89e3\u5bc6\uff0c\u5148\u5bf9\u5bc6\u6587\u89e3\u7801\r\n*\u5982\u679c\u52a0\u5bc6,\u5c06\u5bc6\u7801\u7b97\u5b50\u548c\u5f85\u52a0\u5bc6\u5b57\u7b26\u4e32\u8fdb\u884cmd5\u8fd0\u7b97\u540e\u53d6\u524d8\u4f4d\r\n*\u5e76\u5c06\u8fd98\u4f4d\u5b57\u7b26\u4e32\u548c\u5f85\u52a0\u5bc6\u5b57\u7b26\u4e32\u8fde\u63a5\u6210\u65b0\u7684\u5f85\u52a0\u5bc6\u5b57\u7b26\u4e32\r\n*\/\r\n    $string = $operation == 'DECODE' ? base64_decode($string) : substr(md5($string.$key), 0, 8).$string;\r\n    $string_length = strlen($string);\r\n    $rndkey = $box = array();\r\n    $result = '';\r\n\r\n\/**\r\n*\u521d\u59cb\u5316\u52a0\u5bc6\u53d8\u91cf,$rndkey\u548c$box\r\n*\/\r\n    for($i = 0; $i <= 255; $i++) {\r\n        $rndkey[$i] = ord($key[$i % $key_length]);\r\n        $box[$i] = $i;\r\n    }\r\n\r\n\/**\r\n*$box\u6570\u7ec4\u6253\u6563\u4f9b\u52a0\u5bc6\u7528\r\n*\/\r\n    for($j = $i = 0; $i < 256; $i++) {\r\n        $j = ($j + $box[$i] + $rndkey[$i]) % 256;\r\n        $tmp = $box[$i];\r\n        $box[$i] = $box[$j];\r\n        $box[$j] = $tmp;\r\n    }\r\n\r\n\/**\r\n*$box\u7ee7\u7eed\u6253\u6563,\u5e76\u7528\u5f02\u6216\u8fd0\u7b97\u5b9e\u73b0\u52a0\u5bc6\u6216\u89e3\u5bc6\r\n*\/\r\n    for($a = $j = $i = 0; $i < $string_length; $i++) {\r\n        $a = ($a + 1) % 256;\r\n        $j = ($j + $box[$a]) % 256;\r\n        $tmp = $box[$a];\r\n        $box[$a] = $box[$j];\r\n        $box[$j] = $tmp;\r\n        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));\r\n    }\r\n    if($operation == 'DECODE') {\r\n        if(substr($result, 0, 8) == substr(md5(substr($result, 8).$key), 0, 8)) {\r\n            return substr($result, 8);\r\n        } else {\r\n            return '';\r\n        }\r\n    } else {\r\n        return str_replace('=', '', base64_encode($result));\r\n    }\r\n\r\n}\r\n\r\n<\/pre>\n<p>\u89e3\u5bc6cookie\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre lang='php'>\r\n\/\/\u6b64\u5904\u8981\u5305\u542b\u4e0a\u9762\u7684\u51fd\u6570\u4ee3\u7801\r\n\r\n$auth_key=$_DCACHE['settings']['authkey'];   \/\/\u6b64\u503c\u4e5f\u53ef\u4ee5\u4ececdb_settings\u8868\u4e2d\u67e5\u5230\uff0c\u5373authkey\u5bf9\u5e94\u7684\u503c\r\n$key=md5($auth_key.$_SERVER['HTTP_USER_AGENT']);\r\n$result=authcode($_COOKIE['Fbw_auth'], 'DECODE', $key);\/\/$_COOKIE['Fbw_auth']\u662f\u5f53\u524d\u7528\u6237\u767b\u5f55\u540e\u7684cookie\u503c\r\n\r\necho $_COOKIE['Fbw_auth'];\r\necho \"<br>\";\r\necho $result;\r\n<\/pre>\n<p>\u8f93\u51fa\u7684\u7ed3\u679c\u683c\u5f0f\u662f\u201ce10adc3949ba59abbe56e057f20f883e     2\u201d\uff0c\u5206\u522b\u662f\u7528\u6237\u52a0\u5bc6\u540e\u7684\u5bc6\u7801\u548cID\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u7528\u5730\u5740\uff1ahttp:\/\/bbs.itokit.com\/thread-4009-1-1.html \/** *\u7528\u6237\u4fe1\u606f\u52a0\u5bc6\u89e3\u5bc6\u51fd\u6570 * *\u5f85\u52a0\u5bc6\u5185\u5bb9\u7528\\t\u5206\u5272 *@return String \u52a0\u5bc6\u6216\u89e3\u5bc6\u5b57\u7b26\u4e32 *@param String $string \u5f85\u52a0\u5bc6\u6216\u89e3\u5bc6\u5b57\u7b26\u4e32 *@param String $operation \u64cd\u4f5c\u7c7b\u578b\u5b9a\u4e49 DECODE=\u89e3\u5bc6 ENDODE=\u52a0\u5bc6 *@param String $key \u52a0\u5bc6\u7b97\u5b50 *\/ function authcode($string, $operation, $key = &#8221;) { \/** *\u83b7\u53d6\u5bc6\u7801\u7b97\u5b50,\u5982\u672a\u6307\u5b9a\uff0c\u91c7\u53d6\u7cfb\u7edf\u9ed8\u8ba4\u7b97\u5b50 *\u9ed8\u8ba4\u7b97\u5b50\u662f\u8bba\u575b\u6388\u6743\u7801\u548c\u7528\u6237\u6d4f\u89c8\u5668\u4fe1\u606f\u7684md5\u6563\u5217\u503c *$GLOBALS[&#8216;discuz_auth_key&#8217;]&#8212;-\u5168\u5c40\u53d8\u91cf *\u53d6\u503c\u4e3a:md5($_DCACHE[&#8216;settings&#8217;][&#8216;authkey&#8217;].$_SERVER[&#8216;HTTP_USER_AGENT&#8217;]) *$_DCACHE[&#8216;settings&#8217;][&#8216;authkey&#8217;]\u662f\u8bba\u575b\u5b89\u88c5\u65f6\u751f\u6210\u768415\u4f4d\u968f\u673a\u5b57\u7b26\u4e32 *\/ $key = md5($key &hellip; <a href=\"http:\/\/zduo.me\/blog\/?p=208\">\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":[13,8],"tags":[55,167,166],"_links":{"self":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/208"}],"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=208"}],"version-history":[{"count":5,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":213,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions\/213"}],"wp:attachment":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}