{"id":3012,"date":"2025-03-19T22:03:01","date_gmt":"2025-03-19T14:03:01","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3012"},"modified":"2025-03-19T22:32:35","modified_gmt":"2025-03-19T14:32:35","slug":"php-%e8%a1%a8%e5%8d%95%e9%aa%8c%e8%af%81%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/19\/php-%e8%a1%a8%e5%8d%95%e9%aa%8c%e8%af%81%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97\/","title":{"rendered":"PHP \u8868\u5355\u9a8c\u8bc1"},"content":{"rendered":"\n<p>\u8868\u5355\u9a8c\u8bc1\u662f Web \u5f00\u53d1\u4e2d\u7684\u91cd\u8981\u73af\u8282\uff0c\u5b83\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u7b26\u5408\u9884\u671f\uff0c\u5e76\u9632\u6b62 <strong>SQL \u6ce8\u5165\u3001XSS\uff08\u8de8\u7ad9\u811a\u672c\u653b\u51fb\uff09\u548c CSRF\uff08\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff09<\/strong> \u7b49\u5b89\u5168\u98ce\u9669\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1\ufe0f\u20e3 \u8868\u5355\u9a8c\u8bc1\u7684\u91cd\u8981\u6027<\/strong><\/h2>\n\n\n\n<p>\u2705 <strong>\u9632\u6b62\u7a7a\u503c\u63d0\u4ea4<\/strong>\uff1a\u907f\u514d\u7528\u6237\u63d0\u4ea4\u7a7a\u8868\u5355\u3002<br>\u2705 <strong>\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u6b63\u786e<\/strong>\uff1a\u5982\u90ae\u7bb1\u3001\u7535\u8bdd\u53f7\u7801\u3001URL \u683c\u5f0f\u7b49\u3002<br>\u2705 <strong>\u9632\u6b62 XSS \u548c SQL \u6ce8\u5165<\/strong>\uff1a\u786e\u4fdd\u7528\u6237\u8f93\u5165\u6570\u636e\u4e0d\u4f1a\u88ab\u7528\u4e8e\u6076\u610f\u653b\u51fb\u3002<br>\u2705 <strong>\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c<\/strong>\uff1a\u51cf\u5c11\u7528\u6237\u63d0\u4ea4\u9519\u8bef\uff0c\u63d0\u9ad8\u8868\u5355\u53ef\u7528\u6027\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2\ufe0f\u20e3 HTML \u8868\u5355<\/strong><\/h2>\n\n\n\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5e26\u6709 <strong>\u59d3\u540d\u3001\u90ae\u7bb1\u3001\u7f51\u5740\u3001\u5e74\u9f84<\/strong> \u548c <strong>\u7559\u8a00<\/strong> \u7684 HTML \u8868\u5355\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form action=\"validate.php\" method=\"post\"&gt;\n    &lt;label for=\"name\"&gt;\u59d3\u540d:&lt;\/label&gt;\n    &lt;input type=\"text\" name=\"name\" id=\"name\"&gt;\n\n    &lt;label for=\"email\"&gt;\u90ae\u7bb1:&lt;\/label&gt;\n    &lt;input type=\"text\" name=\"email\" id=\"email\"&gt;\n\n    &lt;label for=\"website\"&gt;\u7f51\u5740:&lt;\/label&gt;\n    &lt;input type=\"text\" name=\"website\" id=\"website\"&gt;\n\n    &lt;label for=\"age\"&gt;\u5e74\u9f84:&lt;\/label&gt;\n    &lt;input type=\"text\" name=\"age\" id=\"age\"&gt;\n\n    &lt;label for=\"comment\"&gt;\u7559\u8a00:&lt;\/label&gt;\n    &lt;textarea name=\"comment\" id=\"comment\"&gt;&lt;\/textarea&gt;\n\n    &lt;input type=\"submit\" value=\"\u63d0\u4ea4\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3\ufe0f\u20e3 PHP \u8868\u5355\u9a8c\u8bc1\u4ee3\u7801<\/strong><\/h2>\n\n\n\n<p>\u521b\u5efa <code>validate.php<\/code> \u8fdb\u884c\u670d\u52a1\u5668\u7aef\u9a8c\u8bc1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nif ($_SERVER&#91;\"REQUEST_METHOD\"] == \"POST\") {\n    \/\/ 1\ufe0f\u20e3 \u5b9a\u4e49\u53d8\u91cf\n    $name = $email = $website = $age = $comment = \"\";\n    $errors = &#91;];\n\n    \/\/ 2\ufe0f\u20e3 \u59d3\u540d\u9a8c\u8bc1\uff08\u5fc5\u586b\uff0c\u81f3\u5c112\u4e2a\u5b57\u7b26\uff09\n    if (empty($_POST&#91;\"name\"])) {\n        $errors&#91;] = \"\u59d3\u540d\u4e0d\u80fd\u4e3a\u7a7a\uff01\";\n    } else {\n        $name = test_input($_POST&#91;\"name\"]);\n        if (!preg_match(\"\/^&#91;a-zA-Z\\x{4e00}-\\x{9fa5} ]+$\/u\", $name)) {\n            $errors&#91;] = \"\u59d3\u540d\u53ea\u80fd\u5305\u542b\u5b57\u6bcd\u548c\u7a7a\u683c\uff01\";\n        }\n    }\n\n    \/\/ 3\ufe0f\u20e3 \u90ae\u7bb1\u9a8c\u8bc1\n    if (empty($_POST&#91;\"email\"])) {\n        $errors&#91;] = \"\u90ae\u7bb1\u4e0d\u80fd\u4e3a\u7a7a\uff01\";\n    } else {\n        $email = test_input($_POST&#91;\"email\"]);\n        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {\n            $errors&#91;] = \"\u65e0\u6548\u7684\u90ae\u7bb1\u683c\u5f0f\uff01\";\n        }\n    }\n\n    \/\/ 4\ufe0f\u20e3 \u7f51\u5740\u9a8c\u8bc1\n    if (!empty($_POST&#91;\"website\"])) {\n        $website = test_input($_POST&#91;\"website\"]);\n        if (!filter_var($website, FILTER_VALIDATE_URL)) {\n            $errors&#91;] = \"\u65e0\u6548\u7684\u7f51\u5740\uff01\";\n        }\n    }\n\n    \/\/ 5\ufe0f\u20e3 \u5e74\u9f84\u9a8c\u8bc1\uff08\u5fc5\u987b\u662f\u6570\u5b57\uff09\n    if (!empty($_POST&#91;\"age\"])) {\n        $age = test_input($_POST&#91;\"age\"]);\n        if (!filter_var($age, FILTER_VALIDATE_INT)) {\n            $errors&#91;] = \"\u5e74\u9f84\u5fc5\u987b\u662f\u6574\u6570\uff01\";\n        }\n    }\n\n    \/\/ 6\ufe0f\u20e3 \u7559\u8a00\uff08\u9632\u6b62 XSS\uff09\n    if (!empty($_POST&#91;\"comment\"])) {\n        $comment = test_input($_POST&#91;\"comment\"]);\n    }\n\n    \/\/ 7\ufe0f\u20e3 \u7edf\u4e00\u6570\u636e\u6e05\u7406\u51fd\u6570\n    function test_input($data) {\n        $data = trim($data);             \/\/ \u53bb\u6389\u524d\u540e\u7a7a\u683c\n        $data = stripslashes($data);     \/\/ \u5220\u9664\u53cd\u659c\u6760\n        $data = htmlspecialchars($data); \/\/ \u8f6c\u6362 HTML \u5b9e\u4f53\uff0c\u9632\u6b62 XSS\n        return $data;\n    }\n\n    \/\/ 8\ufe0f\u20e3 \u663e\u793a\u7ed3\u679c\u6216\u9519\u8bef\u4fe1\u606f\n    if (empty($errors)) {\n        echo \"\u2705 \u9a8c\u8bc1\u6210\u529f\uff01&lt;br&gt;\";\n        echo \"\u59d3\u540d: $name &lt;br&gt;\";\n        echo \"\u90ae\u7bb1: $email &lt;br&gt;\";\n        echo \"\u7f51\u5740: $website &lt;br&gt;\";\n        echo \"\u5e74\u9f84: $age &lt;br&gt;\";\n        echo \"\u7559\u8a00: $comment &lt;br&gt;\";\n    } else {\n        foreach ($errors as $error) {\n            echo \"\u274c $error &lt;br&gt;\";\n        }\n    }\n}\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4\ufe0f\u20e3 \u8be6\u7ec6\u89e3\u6790<\/strong><\/h2>\n\n\n\n<p>\u2705 <code>$_SERVER[\"REQUEST_METHOD\"] == \"POST\"<\/code>\uff1a\u786e\u4fdd\u6570\u636e\u662f <code>POST<\/code> \u63d0\u4ea4\u7684\u3002<br>\u2705 <code>empty($_POST[\"name\"])<\/code>\uff1a\u68c0\u67e5\u662f\u5426\u4e3a\u7a7a\u3002<br>\u2705 <code>preg_match(\"\/^[a-zA-Z\\x{4e00}-\\x{9fa5} ]+$\/u\", $name)<\/code>\uff1a\u68c0\u67e5\u59d3\u540d\u662f\u5426\u53ea\u5305\u542b <strong>\u5b57\u6bcd\u3001\u6c49\u5b57\u548c\u7a7a\u683c<\/strong>\u3002<br>\u2705 <code>filter_var($email, FILTER_VALIDATE_EMAIL)<\/code>\uff1a\u68c0\u67e5\u90ae\u7bb1\u683c\u5f0f\u3002<br>\u2705 <code>filter_var($website, FILTER_VALIDATE_URL)<\/code>\uff1a\u68c0\u67e5\u7f51\u5740\u662f\u5426\u6709\u6548\u3002<br>\u2705 <code>filter_var($age, FILTER_VALIDATE_INT)<\/code>\uff1a\u786e\u4fdd\u5e74\u9f84\u662f\u6574\u6570\u3002<br>\u2705 <code>htmlspecialchars()<\/code>\uff1a\u9632\u6b62 XSS\uff08\u8de8\u7ad9\u811a\u672c\u653b\u51fb\uff09\u3002<br>\u2705 <code>stripslashes()<\/code>\uff1a\u53bb\u6389\u53cd\u659c\u6760\uff0c\u907f\u514d SQL \u6ce8\u5165\u3002<br>\u2705 <code>trim()<\/code>\uff1a\u53bb\u6389\u524d\u540e\u7a7a\u683c\uff0c\u9632\u6b62\u7528\u6237\u8bef\u8f93\u5165\u7a7a\u683c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5\ufe0f\u20e3 \u9632\u6b62 CSRF\uff08\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff09<\/strong><\/h2>\n\n\n\n<p>\u5728 HTML \u8868\u5355\u4e2d\u6dfb\u52a0 CSRF \u4ee4\u724c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nsession_start();\n$_SESSION&#91;\"csrf_token\"] = bin2hex(random_bytes(32));\n?&gt;\n&lt;form action=\"validate.php\" method=\"post\"&gt;\n    &lt;input type=\"hidden\" name=\"csrf_token\" value=\"&lt;?php echo $_SESSION&#91;'csrf_token']; ?&gt;\"&gt;\n    &lt;input type=\"text\" name=\"name\"&gt;\n    &lt;input type=\"submit\" value=\"\u63d0\u4ea4\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<p><strong>\u5728 <code>validate.php<\/code> \u9a8c\u8bc1 CSRF \u4ee4\u724c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>session_start();\nif ($_POST&#91;'csrf_token'] !== $_SESSION&#91;'csrf_token']) {\n    die(\"CSRF \u9a8c\u8bc1\u5931\u8d25\uff01\");\n}<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>\u539f\u7406<\/strong>\uff1a\u6bcf\u4e2a\u8868\u5355\u63d0\u4ea4\u65f6\u9644\u5e26 <code>csrf_token<\/code>\uff0c\u9632\u6b62\u6076\u610f\u7f51\u7ad9\u4f2a\u9020\u8bf7\u6c42\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6\ufe0f\u20e3 \u9632\u6b62\u8868\u5355\u91cd\u590d\u63d0\u4ea4<\/strong><\/h2>\n\n\n\n<p>\u7528\u6237\u5237\u65b0\u9875\u9762\u4f1a\u5bfc\u81f4\u8868\u5355\u91cd\u590d\u63d0\u4ea4\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong><code>header(\"Location: success.php\")<\/code><\/strong> \u89e3\u51b3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if ($_SERVER&#91;\"REQUEST_METHOD\"] == \"POST\") {\n    \/\/ \u5904\u7406\u6570\u636e\n    header(\"Location: success.php\"); \/\/ \u91cd\u5b9a\u5411\uff0c\u907f\u514d\u91cd\u590d\u63d0\u4ea4\n    exit;\n}<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>\u539f\u7406<\/strong>\uff1a\u63d0\u4ea4\u540e\u91cd\u5b9a\u5411\u5230 <code>success.php<\/code>\uff0c\u5237\u65b0\u4e0d\u4f1a\u91cd\u590d\u63d0\u4ea4\u6570\u636e\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7\ufe0f\u20e3 \u4f7f\u7528 JavaScript \u8fdb\u884c\u524d\u7aef\u9a8c\u8bc1<\/strong><\/h2>\n\n\n\n<p>\u867d\u7136\u670d\u52a1\u5668\u7aef\u9a8c\u8bc1\u662f\u5fc5\u987b\u7684\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528 JavaScript \u8fdb\u884c\u524d\u7aef\u9a8c\u8bc1\uff0c\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\nfunction validateForm() {\n    let name = document.getElementById(\"name\").value;\n    let email = document.getElementById(\"email\").value;\n\n    if (name.trim() === \"\") {\n        alert(\"\u59d3\u540d\u4e0d\u80fd\u4e3a\u7a7a\uff01\");\n        return false;\n    }\n\n    let emailPattern = \/^&#91;^\\s@]+@&#91;^\\s@]+\\.&#91;^\\s@]+$\/;\n    if (!email.match(emailPattern)) {\n        alert(\"\u8bf7\u8f93\u5165\u6709\u6548\u7684\u90ae\u7bb1\uff01\");\n        return false;\n    }\n\n    return true;\n}\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>\u524d\u7aef\u9a8c\u8bc1<\/strong>\u53ef\u4ee5 <strong>\u51cf\u5c11\u670d\u52a1\u5668\u8d1f\u62c5<\/strong>\uff0c\u4f46\u4e0d\u80fd\u66ff\u4ee3\u670d\u52a1\u5668\u7aef\u9a8c\u8bc1\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8\ufe0f\u20e3 \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>\u9a8c\u8bc1\u7c7b\u578b<\/strong><\/th><th><strong>\u65b9\u6cd5<\/strong><\/th><\/tr><\/thead><tbody><tr><td>\u5fc5\u586b\u5b57\u6bb5<\/td><td><code>empty($value)<\/code><\/td><\/tr><tr><td>\u5b57\u7b26\u9650\u5236<\/td><td><code>preg_match(\"\/\u6b63\u5219\/\")<\/code><\/td><\/tr><tr><td>\u90ae\u7bb1\u683c\u5f0f<\/td><td><code>filter_var($email, FILTER_VALIDATE_EMAIL)<\/code><\/td><\/tr><tr><td>URL \u683c\u5f0f<\/td><td><code>filter_var($website, FILTER_VALIDATE_URL)<\/code><\/td><\/tr><tr><td>\u6570\u5b57\u683c\u5f0f<\/td><td><code>filter_var($age, FILTER_VALIDATE_INT)<\/code><\/td><\/tr><tr><td>\u9632\u6b62 XSS<\/td><td><code>htmlspecialchars($data)<\/code><\/td><\/tr><tr><td>\u9632\u6b62 CSRF<\/td><td><code>$_SESSION['csrf_token']<\/code><\/td><\/tr><tr><td>\u9632\u6b62\u91cd\u590d\u63d0\u4ea4<\/td><td><code>header(\"Location: success.php\")<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udccc <strong>\u63a8\u8350\u9605\u8bfb<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/tutorial.forms.php\">PHP \u5b98\u65b9\u6587\u6863 &#8211; \u8868\u5355\u9a8c\u8bc1<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/function.filter-var.php\">PHP filter_var() \u53c2\u8003\u624b\u518c<\/a><\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8868\u5355\u9a8c\u8bc1\u662f Web \u5f00\u53d1\u4e2d\u7684\u91cd\u8981\u73af\u8282\uff0c\u5b83\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u7b26\u5408\u9884\u671f\uff0c\u5e76\u9632\u6b62 SQL \u6ce8\u5165\u3001XSS\uff08\u8de8\u7ad9\u811a\u672c\u653b\u51fb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3013,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3012","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3012"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3012\/revisions"}],"predecessor-version":[{"id":3050,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3012\/revisions\/3050"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3013"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}