{"id":3009,"date":"2025-03-19T22:00:26","date_gmt":"2025-03-19T14:00:26","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3009"},"modified":"2025-03-19T22:32:20","modified_gmt":"2025-03-19T14:32:20","slug":"php-%e8%a1%a8%e5%8d%95%e5%92%8c%e7%94%a8%e6%88%b7%e8%be%93%e5%85%a5%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%e5%92%8c%e7%94%a8%e6%88%b7%e8%be%93%e5%85%a5%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97\/","title":{"rendered":"PHP \u8868\u5355\u548c\u7528\u6237\u8f93\u5165"},"content":{"rendered":"\n<p>\u5728 Web \u5f00\u53d1\u4e2d\uff0c\u8868\u5355\uff08Forms\uff09\u662f\u7528\u6237\u4e0e\u670d\u52a1\u5668\u4ea4\u4e92\u7684\u4e3b\u8981\u65b9\u5f0f\u4e4b\u4e00\uff0cPHP \u8d1f\u8d23\u5904\u7406\u7528\u6237\u8f93\u5165\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\">1\ufe0f\u20e3 <strong>HTML \u8868\u5355\u57fa\u7840<\/strong><\/h2>\n\n\n\n<p>PHP \u4f7f\u7528 <strong><code>$_GET<\/code> \u548c <code>$_POST<\/code><\/strong> \u53d8\u91cf\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002<br><strong>HTML \u8868\u5355\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form action=\"process.php\" method=\"post\"&gt;\n    &lt;label for=\"name\"&gt;\u59d3\u540d:&lt;\/label&gt;\n    &lt;input type=\"text\" name=\"name\" id=\"name\" required&gt;\n\n    &lt;label for=\"email\"&gt;\u90ae\u7bb1:&lt;\/label&gt;\n    &lt;input type=\"email\" name=\"email\" id=\"email\" required&gt;\n\n    &lt;input type=\"submit\" value=\"\u63d0\u4ea4\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>\u89e3\u91ca<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>action=\"process.php\"<\/code>\uff1a\u8868\u5355\u63d0\u4ea4\u5230 <code>process.php<\/code> \u5904\u7406\u3002<\/li>\n\n\n\n<li><code>method=\"post\"<\/code>\uff1a\u4f7f\u7528 <code>POST<\/code> \u65b9\u6cd5\u63d0\u4ea4\u6570\u636e\u3002<\/li>\n\n\n\n<li><code>required<\/code>\uff1a\u5b57\u6bb5\u4e0d\u80fd\u4e3a\u7a7a\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 <strong>\u4f7f\u7528 <code>$_POST<\/code> \u5904\u7406\u8868\u5355\u6570\u636e<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nif ($_SERVER&#91;\"REQUEST_METHOD\"] == \"POST\") {\n    $name = $_POST&#91;'name'];\n    $email = $_POST&#91;'email'];\n\n    echo \"\u59d3\u540d: \" . htmlspecialchars($name) . \"&lt;br&gt;\";\n    echo \"\u90ae\u7bb1: \" . htmlspecialchars($email);\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>\u89e3\u91ca<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$_POST['name']<\/code> \u83b7\u53d6\u8868\u5355 <code>name<\/code> \u8f93\u5165\u503c\u3002<\/li>\n\n\n\n<li><code>htmlspecialchars()<\/code> \u9632\u6b62 <strong>XSS\uff08\u8de8\u7ad9\u811a\u672c\u653b\u51fb\uff09<\/strong>\u3002<\/li>\n\n\n\n<li><code>$_SERVER[\"REQUEST_METHOD\"] == \"POST\"<\/code> \u786e\u4fdd\u6570\u636e\u662f\u901a\u8fc7 <code>POST<\/code> \u65b9\u5f0f\u63d0\u4ea4\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3\ufe0f\u20e3 <strong>\u4f7f\u7528 <code>$_GET<\/code> \u5904\u7406 URL \u4f20\u53c2<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;a href=\"process.php?name=Tom&amp;email=tom@example.com\"&gt;\u70b9\u51fb\u67e5\u770b\u4fe1\u606f&lt;\/a&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nif ($_SERVER&#91;\"REQUEST_METHOD\"] == \"GET\") {\n    if (isset($_GET&#91;'name']) &amp;&amp; isset($_GET&#91;'email'])) {\n        echo \"\u59d3\u540d: \" . htmlspecialchars($_GET&#91;'name']) . \"&lt;br&gt;\";\n        echo \"\u90ae\u7bb1: \" . htmlspecialchars($_GET&#91;'email']);\n    } else {\n        echo \"\u7f3a\u5c11\u53c2\u6570\uff01\";\n    }\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>\u89e3\u91ca<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$_GET['name']<\/code> \u901a\u8fc7 URL \u4f20\u53c2\u3002<\/li>\n\n\n\n<li><strong>\u9002\u7528\u4e8e<\/strong> \u67e5\u8be2\u6570\u636e\uff0c\u5982\u641c\u7d22\u5f15\u64ce\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4\ufe0f\u20e3 <strong>\u8868\u5355\u6570\u636e\u9a8c\u8bc1<\/strong><\/h2>\n\n\n\n<p>\u8868\u5355\u6570\u636e\u5fc5\u987b\u7ecf\u8fc7\u9a8c\u8bc1\uff0c\u4ee5\u9632\u6b62 <strong>SQL \u6ce8\u5165\u3001XSS \u653b\u51fb\u3001\u975e\u6cd5\u8f93\u5165\u7b49<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udccc \u8fc7\u6ee4\u975e\u6cd5\u8f93\u5165<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nif ($_SERVER&#91;\"REQUEST_METHOD\"] == \"POST\") {\n    $name = trim($_POST&#91;'name']);\n    $email = trim($_POST&#91;'email']);\n\n    if (empty($name) || empty($email)) {\n        die(\"\u59d3\u540d\u548c\u90ae\u7bb1\u4e0d\u80fd\u4e3a\u7a7a\uff01\");\n    }\n\n    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {\n        die(\"\u65e0\u6548\u7684\u90ae\u7bb1\u683c\u5f0f\uff01\");\n    }\n\n    echo \"\u59d3\u540d: \" . htmlspecialchars($name) . \"&lt;br&gt;\";\n    echo \"\u90ae\u7bb1: \" . htmlspecialchars($email);\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>\u5b89\u5168\u5904\u7406<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>trim()<\/code> \u53bb\u9664\u9996\u5c3e\u7a7a\u683c\u3002<\/li>\n\n\n\n<li><code>empty()<\/code> \u5224\u65ad\u662f\u5426\u4e3a\u7a7a\u3002<\/li>\n\n\n\n<li><code>filter_var($email, FILTER_VALIDATE_EMAIL)<\/code> \u786e\u4fdd\u90ae\u7bb1\u683c\u5f0f\u6b63\u786e\u3002<\/li>\n\n\n\n<li><code>htmlspecialchars()<\/code> \u9632\u6b62 XSS\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5\ufe0f\u20e3 <strong><code>$_REQUEST<\/code> \u53d8\u91cf<\/strong><\/h2>\n\n\n\n<p><code>$_REQUEST<\/code> \u7ed3\u5408 <code>$_GET<\/code> \u548c <code>$_POST<\/code>\uff0c\u4f46<strong>\u4e0d\u63a8\u8350\u7528\u4e8e\u654f\u611f\u6570\u636e<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nif ($_SERVER&#91;\"REQUEST_METHOD\"] == \"POST\") {\n    $name = $_REQUEST&#91;'name'] ?? '\u533f\u540d';\n    echo \"\u59d3\u540d: \" . htmlspecialchars($name);\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>\u8bf4\u660e<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$_REQUEST['name'] ?? '\u533f\u540d'<\/code>\uff1a\u5982\u679c <code>name<\/code> \u4e3a\u7a7a\uff0c\u9ed8\u8ba4\u503c\u4e3a <strong>\u533f\u540d<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6\ufe0f\u20e3 <strong>\u9632\u6b62\u8868\u5355\u91cd\u590d\u63d0\u4ea4<\/strong><\/h2>\n\n\n\n<p>\u7528\u6237\u5237\u65b0\u9875\u9762\u65f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8868\u5355\u91cd\u590d\u63d0\u4ea4\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong><code>header()<\/code> \u91cd\u5b9a\u5411<\/strong> \u89e3\u51b3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nif ($_SERVER&#91;\"REQUEST_METHOD\"] == \"POST\") {\n    $name = htmlspecialchars($_POST&#91;'name']);\n    echo \"\u63d0\u4ea4\u6210\u529f\uff0c\u59d3\u540d\uff1a\" . $name;\n\n    \/\/ \u907f\u514d\u8868\u5355\u91cd\u590d\u63d0\u4ea4\n    header(\"Location: success.php\");\n    exit;\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u2705 <strong><code>header(\"Location: success.php\")<\/code> \u8ba9\u7528\u6237\u8df3\u8f6c\u5230 <code>success.php<\/code>\uff0c\u9632\u6b62\u91cd\u590d\u63d0\u4ea4\u3002<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7\ufe0f\u20e3 <strong>\u6587\u4ef6\u4e0a\u4f20<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udccc HTML \u6587\u4ef6\u4e0a\u4f20\u8868\u5355<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form action=\"upload.php\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\n    &lt;label for=\"file\"&gt;\u4e0a\u4f20\u6587\u4ef6:&lt;\/label&gt;\n    &lt;input type=\"file\" name=\"file\" id=\"file\"&gt;\n    &lt;input type=\"submit\" value=\"\u4e0a\u4f20\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udccc \u5904\u7406\u4e0a\u4f20<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nif ($_SERVER&#91;\"REQUEST_METHOD\"] == \"POST\") {\n    if (isset($_FILES&#91;\"file\"]) &amp;&amp; $_FILES&#91;\"file\"]&#91;\"error\"] == 0) {\n        $allowedTypes = &#91;\"image\/jpeg\", \"image\/png\", \"image\/gif\"];\n        $fileType = $_FILES&#91;\"file\"]&#91;\"type\"];\n\n        if (!in_array($fileType, $allowedTypes)) {\n            die(\"\u4ec5\u652f\u6301 JPG\u3001PNG \u548c GIF \u683c\u5f0f\uff01\");\n        }\n\n        $uploadDir = \"uploads\/\";\n        $filePath = $uploadDir . basename($_FILES&#91;\"file\"]&#91;\"name\"]);\n\n        if (move_uploaded_file($_FILES&#91;\"file\"]&#91;\"tmp_name\"], $filePath)) {\n            echo \"\u6587\u4ef6\u4e0a\u4f20\u6210\u529f: \" . $filePath;\n        } else {\n            echo \"\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25\uff01\";\n        }\n    } else {\n        echo \"\u8bf7\u9009\u62e9\u6587\u4ef6\uff01\";\n    }\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>\u8bf4\u660e<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$_FILES[\"file\"][\"error\"] == 0<\/code> \u786e\u4fdd\u65e0\u9519\u8bef\u3002<\/li>\n\n\n\n<li><code>in_array($fileType, $allowedTypes)<\/code> \u9650\u5236\u6587\u4ef6\u683c\u5f0f\u3002<\/li>\n\n\n\n<li><code>move_uploaded_file()<\/code> \u79fb\u52a8\u6587\u4ef6\u5230 <code>uploads\/<\/code> \u76ee\u5f55\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8\ufe0f\u20e3 <strong>\u9632\u6b62 CSRF\uff08\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff09<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udccc \u751f\u6210 CSRF \u4ee4\u724c<\/strong><\/h3>\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=\"process.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<h3 class=\"wp-block-heading\"><strong>\ud83d\udccc \u9a8c\u8bc1 CSRF \u4ee4\u724c<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nsession_start();\nif ($_SERVER&#91;\"REQUEST_METHOD\"] == \"POST\") {\n    if (!isset($_POST&#91;'csrf_token']) || $_POST&#91;'csrf_token'] !== $_SESSION&#91;'csrf_token']) {\n        die(\"\u975e\u6cd5\u8bf7\u6c42\uff01\");\n    }\n    echo \"\u5b89\u5168\u63d0\u4ea4\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>\u8bf4\u660e<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$_SESSION['csrf_token']<\/code> \u751f\u6210\u5b89\u5168\u4ee4\u724c\u3002<\/li>\n\n\n\n<li><strong>\u9a8c\u8bc1\u4ee4\u724c<\/strong>\uff0c\u786e\u4fdd\u8bf7\u6c42\u6765\u81ea\u5408\u6cd5\u9875\u9762\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd1f <strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>\u529f\u80fd<\/strong><\/th><th><strong>\u65b9\u6cd5<\/strong><\/th><\/tr><\/thead><tbody><tr><td>\u5904\u7406\u8868\u5355\u6570\u636e<\/td><td><code>$_POST['name']<\/code> \u6216 <code>$_GET['name']<\/code><\/td><\/tr><tr><td>\u8fc7\u6ee4\u8f93\u5165<\/td><td><code>htmlspecialchars()<\/code>\u3001<code>filter_var()<\/code><\/td><\/tr><tr><td>\u9a8c\u8bc1\u90ae\u7bb1<\/td><td><code>FILTER_VALIDATE_EMAIL<\/code><\/td><\/tr><tr><td>\u9632\u6b62\u91cd\u590d\u63d0\u4ea4<\/td><td><code>header(\"Location: success.php\")<\/code><\/td><\/tr><tr><td>\u6587\u4ef6\u4e0a\u4f20<\/td><td><code>move_uploaded_file()<\/code><\/td><\/tr><tr><td>\u9632\u6b62 CSRF<\/td><td>\u4ee4\u724c\u9a8c\u8bc1<\/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\u5904\u7406<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/features.file-upload.php\">PHP \u6587\u4ef6\u4e0a\u4f20<\/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>\u5728 Web \u5f00\u53d1\u4e2d\uff0c\u8868\u5355\uff08Forms\uff09\u662f\u7528\u6237\u4e0e\u670d\u52a1\u5668\u4ea4\u4e92\u7684\u4e3b\u8981\u65b9\u5f0f\u4e4b\u4e00\uff0cPHP \u8d1f\u8d23\u5904\u7406\u7528\u6237\u8f93\u5165\u6570\u636e\u3002 1\ufe0f\u20e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3010,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3009","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\/3009","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=3009"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3009\/revisions"}],"predecessor-version":[{"id":3048,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3009\/revisions\/3048"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3010"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}