{"id":47470,"date":"2024-12-02T08:04:52","date_gmt":"2024-12-02T00:04:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47470\/"},"modified":"2024-12-02T08:04:52","modified_gmt":"2024-12-02T00:04:52","slug":"php-%e5%87%bd%e6%95%b0%e7%9a%84%e5%ae%89%e5%85%a8%e6%80%a7%e5%a2%9e%e5%bc%ba%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47470\/","title":{"rendered":"PHP \u51fd\u6570\u7684\u5b89\u5168\u6027\u589e\u5f3a\u5b9e\u8df5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u7684\u5b89\u5168\u6027\u589e\u5f3a\u5b9e\u8df5<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u51fd\u6570\u7684\u5b89\u5168\u6027\u589e\u5f3a\u5b9e\u8df5\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p>PHP \u51fd\u6570\u7684\u5b89\u5168\u589e\u5f3a\u5b9e\u8df5\uff1a\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\uff0c\u5982\uff1a\u4f7f\u7528 filter_var() \u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002\u5bf9\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u8fdb\u884c\u7f16\u7801\uff0c\u5982\uff1a\u4f7f\u7528 htmlspecialchars() \u7f16\u7801 HTML \u8f93\u51fa\u3002\u9650\u5236\u6570\u636e\u5e93\u8bbf\u95ee\uff0c\u5982\uff1a\u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\u9632\u5fa1 SQL \u6ce8\u5165\u3002\u9632\u8303 CSRF \u653b\u51fb\uff0c\u5982\uff1a\u5728\u8868\u5355\u4e2d\u5305\u542b CSRF \u4ee4\u724c\u3002\u9650\u65f6\u51fd\u6570\u6267\u884c\uff0c\u5982\uff1a\u4f7f\u7528 set_time_limit() \u8bbe\u7f6e\u8d85\u65f6\u3002\u907f\u514d\u4f7f\u7528\u4e0d\u5b89\u5168\u7684\u51fd\u6570\uff0c\u5982\uff1a\u4f7f\u7528 json_decode() \u4ee3\u66ff eval()\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729919559671c7a4779937.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u7684\u5b89\u5168\u6027\u589e\u5f3a\u5b9e\u8df5\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u7684\u5b89\u5168\u6027\u589e\u5f3a\u5b9e\u8df5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u7684\u5b89\u5168\u6027\u589e\u5f3a\u5b9e\u8df5<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>PHP \u51fd\u6570\u662f\u5e94\u7528\u7a0b\u5e8f\u7684\u5173\u952e\u7ec4\u4ef6\uff0c\u786e\u4fdd\u5176\u5b89\u5168\u6027\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u63d0\u4f9b\u589e\u5f3a PHP \u51fd\u6570\u5b89\u5168\u6027\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u4ee5\u53ca\u4e00\u4e9b\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p><strong>1. \u8f93\u5165\u9a8c\u8bc1<\/strong><\/p>\n<ul>\n<li>\u9a8c\u8bc1\u6765\u81ea\u7528\u6237\u6216\u5176\u4ed6\u6765\u6e90\u7684\u6240\u6709\u8f93\u5165\u3002<\/li>\n<li>\u4f7f\u7528 <code>filter_var()<\/code>\u3001<code>preg_match()<\/code> \u7b49\u51fd\u6570\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u548c\u9a8c\u8bc1\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528\u57fa\u4e8e\u4e0a\u4e0b\u6587\u7684\u8f93\u5165\u9a8c\u8bc1\u5e93\uff0c\u4f8b\u5982 <code>Respect\/Validation<\/code>\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\n$email = filter_var($_GET['email'], FILTER_VALIDATE_EMAIL);\nif (!$email) {\n    throw new InvalidEmailException();\n}<\/pre>\n<p><strong>2. \u8f93\u51fa\u7f16\u7801<\/strong><\/p>\n<ul>\n<li>\u5bf9\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u8fdb\u884c\u7f16\u7801\uff0c\u4f8b\u5982 HTML\u3001JSON \u548c XML\u3002<\/li>\n<li>\u4f7f\u7528 <code>htmlspecialchars()<\/code>\u3001<code>json_encode()<\/code> \u7b49\u51fd\u6570\u8fdb\u884c\u7f16\u7801\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528\u4e13\u4e1a\u7684\u7f16\u7801\u5e93\uff0c\u4f8b\u5982 <code>HTMLPurifier<\/code>\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u7f16\u7801 HTML \u8f93\u51fa\u4ee5\u9632\u6b62\u8de8\u7ad9\u70b9\u811a\u672c\u653b\u51fb\necho htmlspecialchars($output);<\/pre>\n<p><strong>3. \u9650\u5236\u6570\u636e\u5e93\u8bbf\u95ee<\/strong><\/p>\n<ul>\n<li>\u53ea\u6388\u4e88\u51fd\u6570\u6700\u5c11\u5fc5\u8981\u7684\u6570\u636e\u5e93\u6743\u9650\u3002<\/li>\n<li>\u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\u6765\u9632\u5fa1 SQL \u6ce8\u5165\u653b\u51fb\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528 ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5668\uff09\u5e93\uff0c\u4f8b\u5982 <code>Doctrine<\/code>\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\u7684 SQL \u67e5\u8be2\n$stmt = $conn-&gt;prepare(\"SELECT * FROM users WHERE username = :username\");\n$stmt-&gt;bindParam(':username', $username);\n$stmt-&gt;execute();<\/pre>\n<p><strong>4. \u9632\u8303 CSRF \u653b\u51fb<\/strong><\/p>\n<ul>\n<li>\u5728\u6240\u6709\u8868\u5355\u4e2d\u5305\u542b CSRF \u4ee4\u724c\u3002<\/li>\n<li>\u9a8c\u8bc1\u8bf7\u6c42\u4e2d\u7684 CSRF \u4ee4\u724c\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528 CSRF \u4fdd\u62a4\u6846\u67b6\uff0c\u4f8b\u5982 <code>Symfony\\Component\\Security\\Csrf\\CsrfTokenManager<\/code>\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u6dfb\u52a0 CSRF \u4ee4\u724c\u5230\u8868\u5355\necho '&lt;input type=\"hidden\" name=\"_csrf_token\" value=\"' . $token . '\"&gt;';<\/pre>\n<p><strong>5. \u9650\u65f6\u51fd\u6570\u6267\u884c<\/strong><\/p>\n<ul>\n<li>\u4e3a\u6267\u884c\u8f83\u957f\u7684\u51fd\u6570\u8bbe\u7f6e\u8d85\u65f6\u3002<\/li>\n<li>\u4f7f\u7528 <code>set_time_limit()<\/code> \u51fd\u6570\u6216 <code>pcntl_alarm()<\/code> \u51fd\u6570\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528\u4e00\u4e2a\u5916\u90e8\u9875\u9762\u6765\u5904\u7406\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u8bbe\u7f6e\u811a\u672c\u7684\u6700\u957f\u6267\u884c\u65f6\u95f4\u4e3a 30 \u79d2\nset_time_limit(30);<\/pre>\n<p><strong>6. \u907f\u514d\u4f7f\u7528\u4e0d\u5b89\u5168\u7684\u51fd\u6570<\/strong><\/p>\n<ul>\n<li>\u907f\u514d\u4f7f\u7528 <code>eval()<\/code>\u3001<code>system()<\/code> \u7b49\u4e0d\u5b89\u5168\u7684\u51fd\u6570\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528\u66ff\u4ee3\u65b9\u6cd5\uff0c\u4f8b\u5982 <code>json_decode()<\/code>\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u907f\u514d\u4f7f\u7528 eval()\n$data = json_decode($_POST['data']); \/\/ \u4f7f\u7528 json_decode() \u4ee3\u66ff<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u60a8\u53ef\u4ee5\u663e\u8457\u589e\u5f3a PHP \u51fd\u6570\u7684\u5b89\u5168\u6027\u3002\u8fd9\u4e9b\u5b9e\u6218\u6848\u4f8b\u63d0\u4f9b\u4e86\u5c06\u8fd9\u4e9b\u6982\u5ff5\u5e94\u7528\u4e8e\u5b9e\u9645\u5e94\u7528\u7a0b\u5e8f\u7684\u5177\u4f53\u793a\u4f8b\u3002\u901a\u8fc7\u8c28\u614e\u5730\u5b9e\u65bd\u8fd9\u4e9b\u63aa\u65bd\uff0c\u60a8\u5c06\u80fd\u591f\u4fdd\u62a4\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u514d\u53d7\u6f5c\u5728\u7684\u653b\u51fb\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPHP \u51fd\u6570\u7684\u5b89\u5168\u6027\u589e\u5f3a\u5b9e\u8df5\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u7684\u5b89\u5168\u6027\u589e\u5f3a\u5b9e\u8df5 \u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u51fd\u6570\u7684\u5b89\u5168\u6027\u589e\u5f3a\u5b9e\u8df5\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 PHP \u51fd\u6570\u7684\u5b89\u5168\u589e\u5f3a\u5b9e\u8df5\uff1a\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\uff0c\u5982\uff1a\u4f7f\u7528 filter_var() \u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002\u5bf9\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u8fdb\u884c\u7f16\u7801\uff0c\u5982\uff1a\u4f7f\u7528 htmlspecialchars() \u7f16\u7801 HTML \u8f93\u51fa\u3002\u9650\u5236\u6570\u636e\u5e93\u8bbf\u95ee\uff0c\u5982\uff1a\u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\u9632\u5fa1 SQL \u6ce8\u5165\u3002\u9632\u8303 CSRF \u653b\u51fb\uff0c\u5982\uff1a\u5728\u8868\u5355\u4e2d\u5305\u542b CSRF \u4ee4\u724c\u3002\u9650\u65f6\u51fd\u6570\u6267\u884c\uff0c\u5982\uff1a\u4f7f\u7528 set_time_limit() \u8bbe\u7f6e\u8d85\u65f6\u3002\u907f\u514d\u4f7f\u7528\u4e0d\u5b89\u5168\u7684\u51fd\u6570\uff0c\u5982\uff1a\u4f7f\u7528 json_decode() \u4ee3\u66ff eval()\u3002 PHP \u51fd\u6570\u7684\u5b89\u5168\u6027\u589e\u5f3a\u5b9e\u8df5 \u524d\u8a00 PHP \u51fd\u6570\u662f\u5e94\u7528\u7a0b\u5e8f\u7684\u5173\u952e\u7ec4\u4ef6\uff0c\u786e\u4fdd\u5176\u5b89\u5168\u6027\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u63d0\u4f9b\u589e\u5f3a PHP \u51fd\u6570\u5b89\u5168\u6027\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u4ee5\u53ca\u4e00\u4e9b\u5b9e\u6218\u6848\u4f8b\u3002 \u6700\u4f73\u5b9e\u8df5 1. \u8f93\u5165\u9a8c\u8bc1 \u9a8c\u8bc1\u6765\u81ea\u7528\u6237\u6216\u5176\u4ed6\u6765\u6e90\u7684\u6240\u6709\u8f93\u5165\u3002 \u4f7f\u7528 filter_var()\u3001preg_match() \u7b49\u51fd\u6570\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u548c\u9a8c\u8bc1\u3002 \u8003\u8651\u4f7f\u7528\u57fa\u4e8e\u4e0a\u4e0b\u6587\u7684\u8f93\u5165\u9a8c\u8bc1\u5e93\uff0c\u4f8b\u5982 Respect\/Validation\u3002 \u5b9e\u6218\u6848\u4f8b\uff1a \/\/ \u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740 $email = filter_var($_GET[&#8217;email&#8217;], FILTER_VALIDATE_EMAIL); if (!$email) { throw new InvalidEmailException(); } 2. \u8f93\u51fa\u7f16\u7801 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47470","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47470"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47470\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}