{"id":3009,"date":"2024-07-22T09:55:55","date_gmt":"2024-07-22T01:55:55","guid":{"rendered":"https:\/\/www.tra56.com\/?p=3009"},"modified":"2024-07-22T09:55:55","modified_gmt":"2024-07-22T01:55:55","slug":"3009","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/3009.html","title":{"rendered":"php\u7684api\u63a5\u53e3token\u7b80\u5355\u5b9e\u73b0"},"content":{"rendered":"
<\/p>\n
<?php\r\n\/\/ \u751f\u6210 Token\r\nfunction generateToken() {\r\n $token = bin2hex(random_bytes(16)); \/\/ \u4f7f\u7528\u968f\u673a\u5b57\u8282\u751f\u6210 token\r\n return $token;\r\n}\r\n\/\/ \u5b58\u50a8 Token\uff08\u8fd9\u91cc\u4f7f\u7528\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u6765\u6a21\u62df\u5b58\u50a8\uff09\r\n$tokens = [];\r\n\/\/ \u9a8c\u8bc1 Token\r\nfunction validateToken($token) {\r\n global $tokens;\r\n \/\/ \u68c0\u67e5 token \u662f\u5426\u5b58\u5728\u4e8e\u5b58\u50a8\u4e2d\r\n if (in_array($token, $tokens)) {\r\n return true;\r\n }\r\n return false;\r\n}\r\n\/\/ \u7528\u6237\u767b\u5f55\uff08\u793a\u4f8b\uff0c\u5b9e\u9645\u4f7f\u7528\u65f6\u8981\u8fde\u63a5\u6570\u636e\u5e93\u7b49\u9a8c\u8bc1\u903b\u8f91\uff09\r\nfunction login($username, $password) {\r\n if ($username === 'admin' && $password === 'password') {\r\n \/\/ \u767b\u5f55\u6210\u529f\uff0c\u751f\u6210 token\uff0c\u5e76\u5b58\u50a8\u5728\u670d\u52a1\u5668\u7aef\r\n $token = generateToken();\r\n global $tokens;\r\n $tokens[] = $token;\r\n return $token;\r\n }\r\n return false;\r\n}\r\n\/\/ \u793a\u4f8b\u4f7f\u7528\u65b9\u6cd5\r\n$username = 'admin';\r\n$password = 'password';\r\n$token = login($username, $password);\r\nif ($token) {\r\n \/\/ \u767b\u5f55\u6210\u529f\uff0c\u8fd4\u56de token \u7ed9\u5ba2\u6237\u7aef\r\n echo "Token: " . $token;\r\n} else {\r\n \/\/ \u767b\u5f55\u5931\u8d25\r\n echo "\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\uff01";\r\n}\r\n\/\/ \u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u5728\u8bf7\u6c42\u5934\u4e2d\u9644\u5e26 token\r\n\/\/ \u4f8b\u5982\uff1aAuthorization: Bearer <token>\r\n\/\/ \u670d\u52a1\u5668\u7aef\u63a5\u6536\u8bf7\u6c42\u540e\uff0c\u9a8c\u8bc1 token\r\n\/\/ \u4ece\u8bf7\u6c42\u5934\u4e2d\u83b7\u53d6 token\r\n$authorizationHeader = $_SERVER['HTTP_AUTHORIZATION'];\r\nlist(, $token) = explode(' ', $authorizationHeader);\r\n\/\/ \u9a8c\u8bc1 token\r\nif (validateToken($token)) {\r\n echo "Token \u9a8c\u8bc1\u901a\u8fc7";\r\n} else {\r\n echo "Token \u9a8c\u8bc1\u5931\u8d25";\r\n}\r\n?><\/pre>\n\u4e0a\u9762\u662f\u7b80\u5355token\u5b9e\u73b0<\/span><\/p>\n
<\/span><\/p>\n\u5b9e\u9645\u8fc7\u7a0b\u4e2d\u5b8c\u6574\u65b9\u6848<\/span><\/p>\n
<\/span><\/p>\n\u751f\u6210 Token\uff1a\u5728\u7528\u6237\u767b\u5f55\u6216\u6ce8\u518c\u6210\u529f\u540e\uff0c\u4f7f\u7528\u4e00\u79cd\u5b89\u5168\u7684\u7b97\u6cd5\uff08\u4f8b\u5982 HMAC\uff09\u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684\u5b57\u7b26\u4e32\u4f5c\u4e3a token\u3002<\/span><\/p>\n
\u5b58\u50a8 Token\uff1a\u5c06\u751f\u6210\u7684 token \u5b58\u50a8\u5728\u670d\u52a1\u5668\u7aef\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\u3001\u7f13\u5b58\u6216\u5176\u4ed6\u6301\u4e45\u5316\u65b9\u5f0f\u3002<\/span><\/p>\n
\u8fd4\u56de Token\uff1a\u5c06\u751f\u6210\u7684 token \u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06 token \u4f5c\u4e3a\u54cd\u5e94\u7684\u4e00\u90e8\u5206\u6216\u8005\u5728\u54cd\u5e94\u5934\u4e2d\u8fd4\u56de\u3002<\/span><\/p>\n
\u9a8c\u8bc1 Token\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u5c06 token \u653e\u5728\u8bf7\u6c42\u7684\u5934\u90e8\u3001URL \u53c2\u6570\u6216\u8005\u8bf7\u6c42\u4f53\u4e2d\u3002\u670d\u52a1\u5668\u7aef\u63a5\u6536\u5230\u8bf7\u6c42\u540e\uff0c\u5148\u83b7\u53d6 token\uff0c\u7136\u540e\u6839\u636e\u5b58\u50a8\u7684 token \u8fdb\u884c\u9a8c\u8bc1\u3002\u9a8c\u8bc1\u7684\u65b9\u5f0f\u53ef\u4ee5\u662f\u6bd4\u8f83\u5ba2\u6237\u7aef\u63d0\u4f9b\u7684 token \u548c\u670d\u52a1\u5668\u7aef\u5b58\u50a8\u7684 token \u662f\u5426\u4e00\u81f4\u3002<\/span><\/p>\n
\u5237\u65b0 Token\uff1a\u4e3a\u4e86\u63d0\u9ad8\u5b89\u5168\u6027\uff0c\u53ef\u4ee5\u5b9a\u671f\u5237\u65b0 token\u3002\u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u68c0\u67e5 token \u662f\u5426\u5373\u5c06\u8fc7\u671f\u3002\u5982\u679c\u5373\u5c06\u8fc7\u671f\uff0c\u670d\u52a1\u5668\u7aef\u751f\u6210\u4e00\u4e2a\u65b0\u7684 token\uff0c\u5e76\u66ff\u6362\u6389\u65e7\u7684 token\u3002\u7136\u540e\u5c06\u65b0\u7684 token \u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\uff0c\u5ba2\u6237\u7aef\u5b58\u50a8\u5e76\u5728\u4e0b\u4e00\u6b21\u8bf7\u6c42\u65f6\u4f7f\u7528\u65b0\u7684 token\u3002<\/span><\/p>\n
\u64a4\u9500 Token\uff1a\u5982\u679c\u7528\u6237\u9700\u8981\u6ce8\u9500\u6216\u8005 token \u6cc4\u9732\uff0c\u53ef\u4ee5\u64a4\u9500 token\u3002\u670d\u52a1\u5668\u7aef\u5220\u9664\u5b58\u50a8\u7684 token\uff0c\u4f7f\u5176\u5931\u6548\u3002 \u8fd9\u4e9b\u6b65\u9aa4\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728 PHP \u4e2d\u5b9e\u73b0 API \u7684 token \u9a8c\u8bc1\u673a\u5236\u3002\u8bf7\u6ce8\u610f\uff0c\u5b89\u5168\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u5408\u9002\u7684\u52a0\u5bc6\u7b97\u6cd5\u548c\u5b89\u5168\u63aa\u65bd\u6765\u4fdd\u62a4 token \u7684\u5b89\u5168\u6027\u3002<\/span><\/p>\n
<\/p>\n
<\/p>\n","protected":false},"excerpt":{"rendered":"
<?php \/\/ \u751f\u6210 Token function generateTo […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92],"tags":[],"class_list":["post-3009","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/3009"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=3009"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/3009\/revisions"}],"predecessor-version":[{"id":3010,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/3009\/revisions\/3010"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=3009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=3009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=3009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}