{"id":3028,"date":"2025-03-19T22:12:16","date_gmt":"2025-03-19T14:12:16","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3028"},"modified":"2025-03-19T22:12:16","modified_gmt":"2025-03-19T14:12:16","slug":"php-_post-%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/19\/php-_post-%e5%8f%98%e9%87%8f\/","title":{"rendered":"PHP $_POST \u53d8\u91cf"},"content":{"rendered":"\n<p><code>$_POST<\/code> \u662f PHP \u8d85\u7ea7\u5168\u5c40\u53d8\u91cf\u4e4b\u4e00\uff0c\u7528\u4e8e\u901a\u8fc7 HTTP POST \u65b9\u6cd5\u5411\u670d\u52a1\u5668\u53d1\u9001\u6570\u636e\u3002\u4e0e <code>$_GET<\/code> \u4e0d\u540c\uff0c<code>$_POST<\/code> \u5c06\u6570\u636e\u653e\u5728 HTTP \u8bf7\u6c42\u7684\u6b63\u6587\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u9644\u52a0\u5230 URL \u4e2d\u3002\u56e0\u6b64\uff0c<code>$_POST<\/code> \u9002\u5408\u53d1\u9001\u5927\u91cf\u6570\u636e\u6216\u654f\u611f\u6570\u636e\uff08\u5982\u5bc6\u7801\uff09\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 <code>$_POST<\/code> \u53d8\u91cf\u7684\u57fa\u672c\u7528\u6cd5<\/strong><\/h2>\n\n\n\n<p><code>$_POST<\/code> \u5141\u8bb8\u4f60\u5728 PHP \u811a\u672c\u4e2d\u8bbf\u95ee\u901a\u8fc7 POST \u65b9\u6cd5\u63d0\u4ea4\u7684\u8868\u5355\u6570\u636e\u3002\u5f53\u4f60\u4f7f\u7528\u8868\u5355\u63d0\u4ea4\u6570\u636e\u65f6\uff0c\u6d4f\u89c8\u5668\u4f1a\u5c06\u6570\u636e\u5305\u542b\u5728 HTTP \u8bf7\u6c42\u7684\u6b63\u6587\u4e2d\uff0c\u5e76\u7531 PHP \u811a\u672c\u901a\u8fc7 <code>$_POST<\/code> \u53d8\u91cf\u8fdb\u884c\u63a5\u6536\u548c\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b 1\uff1a\u901a\u8fc7\u8868\u5355\u63d0\u4ea4\u6570\u636e\u5e76\u4f7f\u7528 <code>$_POST<\/code> \u5904\u7406<\/strong><\/h3>\n\n\n\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a HTML \u8868\u5355\uff0c\u5141\u8bb8\u7528\u6237\u8f93\u5165\u59d3\u540d\u548c\u5e74\u9f84\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>HTML \u8868\u5355<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form action=\"index.php\" method=\"post\"&gt;\n    \u59d3\u540d: &lt;input type=\"text\" name=\"name\"&gt;\n    \u5e74\u9f84: &lt;input type=\"text\" name=\"age\"&gt;\n    &lt;input type=\"submit\" value=\"\u63d0\u4ea4\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>PHP \u5904\u7406\u811a\u672c\uff08index.php\uff09<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u68c0\u67e5\u8868\u5355\u6570\u636e\u662f\u5426\u5df2\u63d0\u4ea4\nif ($_SERVER&#91;'REQUEST_METHOD'] == 'POST') {\n    \/\/ \u83b7\u53d6\u8868\u5355\u6570\u636e\n    $name = $_POST&#91;'name'];\n    $age = $_POST&#91;'age'];\n\n    echo \"\u59d3\u540d: \" . $name . \"&lt;br&gt;\";\n    echo \"\u5e74\u9f84: \" . $age . \"&lt;br&gt;\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u5f53\u4f60\u586b\u5199\u8868\u5355\u5e76\u63d0\u4ea4\u65f6\uff0c<code>$_POST<\/code> \u53d8\u91cf\u5c06\u5305\u542b\u4ece\u8868\u5355\u63d0\u4ea4\u7684\u6570\u636e\uff0c\u5e76\u5728\u9875\u9762\u4e0a\u663e\u793a\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 <code>$_POST<\/code> \u4e0e <code>$_GET<\/code> \u7684\u5bf9\u6bd4<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u70b9<\/th><th><code>$_POST<\/code><\/th><th><code>$_GET<\/code><\/th><\/tr><\/thead><tbody><tr><td>\u6570\u636e\u4f20\u8f93\u65b9\u5f0f<\/td><td>HTTP \u8bf7\u6c42\u6b63\u6587<\/td><td>URL \u67e5\u8be2\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>\u6570\u636e\u957f\u5ea6\u9650\u5236<\/td><td>\u7406\u8bba\u4e0a\u6ca1\u6709\u9650\u5236<\/td><td>URL \u957f\u5ea6\u9650\u5236\uff08\u5927\u7ea6 2000 \u5b57\u7b26\uff09<\/td><\/tr><tr><td>\u6570\u636e\u5b89\u5168\u6027<\/td><td>\u6570\u636e\u4e0d\u66b4\u9732\u5728 URL \u4e2d<\/td><td>\u6570\u636e\u66b4\u9732\u5728 URL \u4e2d<\/td><\/tr><tr><td>\u4f7f\u7528\u573a\u666f<\/td><td>\u63d0\u4ea4\u654f\u611f\u4fe1\u606f\u3001\u5927\u91cf\u6570\u636e<\/td><td>\u67e5\u8be2\u6570\u636e\u3001\u7b80\u5355\u8868\u5355<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7531\u4e8e <code>$_POST<\/code> \u5728 HTTP \u8bf7\u6c42\u7684\u6b63\u6587\u4e2d\u4f20\u9012\u6570\u636e\uff0c\u7528\u6237\u65e0\u6cd5\u76f4\u63a5\u770b\u5230\u6570\u636e\uff0c\u56e0\u6b64\u5b83\u9002\u5408\u63d0\u4ea4\u5bc6\u7801\u3001\u6587\u4ef6\u6216\u5927\u91cf\u6570\u636e\uff0c\u800c <code>$_GET<\/code> \u66f4\u9002\u5408\u7528\u4e8e\u4f20\u9012\u67e5\u8be2\u53c2\u6570\u6216\u7b80\u5355\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>3\ufe0f\u20e3 \u5904\u7406\u6587\u4ef6\u4e0a\u4f20<\/strong><\/h2>\n\n\n\n<p><code>$_POST<\/code> \u4e0d\u4ec5\u9002\u7528\u4e8e\u6587\u672c\u6570\u636e\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u3002\u6587\u4ef6\u901a\u8fc7 HTML \u8868\u5355\u63d0\u4ea4\u65f6\uff0c\u5fc5\u987b\u8bbe\u7f6e <code>enctype=\"multipart\/form-data\"<\/code>\uff0c\u4ee5\u4fbf\u6d4f\u89c8\u5668\u6b63\u786e\u7f16\u7801\u6587\u4ef6\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>HTML \u8868\u5355\uff08\u6587\u4ef6\u4e0a\u4f20\uff09<\/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    \u9009\u62e9\u6587\u4ef6: &lt;input type=\"file\" name=\"fileToUpload\"&gt;\n    &lt;input type=\"submit\" value=\"\u4e0a\u4f20\u6587\u4ef6\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>PHP \u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff08upload.php\uff09<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nif ($_SERVER&#91;'REQUEST_METHOD'] == 'POST') {\n    $target_dir = \"uploads\/\";\n    $target_file = $target_dir . basename($_FILES&#91;\"fileToUpload\"]&#91;\"name\"]);\n\n    \/\/ \u68c0\u67e5\u662f\u5426\u4e3a\u6709\u6548\u7684\u6587\u4ef6\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u56fe\u7247\uff09\n    if (move_uploaded_file($_FILES&#91;\"fileToUpload\"]&#91;\"tmp_name\"], $target_file)) {\n        echo \"\u6587\u4ef6 \" . basename($_FILES&#91;\"fileToUpload\"]&#91;\"name\"]) . \" \u4e0a\u4f20\u6210\u529f\uff01\";\n    } else {\n        echo \"\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25\uff01\";\n    }\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>$_FILES<\/code> \u6570\u7ec4\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff0c\u800c <code>$_POST<\/code> \u4e3b\u8981\u7528\u4e8e\u5904\u7406\u5176\u4ed6\u8868\u5355\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>4\ufe0f\u20e3 \u5b89\u5168\u6027\u4e0e\u6570\u636e\u9a8c\u8bc1<\/strong><\/h2>\n\n\n\n<p><code>$_POST<\/code> \u7684\u6570\u636e\u5e76\u4e0d\u66b4\u9732\u5728 URL \u4e2d\uff0c\u76f8\u5bf9\u6765\u8bf4\u66f4\u4e3a\u5b89\u5168\uff0c\u4f46\u4f9d\u7136\u9700\u8981\u8fdb\u884c\u9a8c\u8bc1\u548c\u6e05\u7406\uff0c\u4ee5\u9632\u6b62 XSS\uff08\u8de8\u7ad9\u811a\u672c\u653b\u51fb\uff09\u548c SQL \u6ce8\u5165\u7b49\u5b89\u5168\u95ee\u9898\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5e38\u7528\u7684\u9a8c\u8bc1\u4e0e\u6e05\u7406\u65b9\u6cd5\uff1a<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9a8c\u8bc1\u6570\u636e\uff1a<\/strong> \u4f7f\u7528 PHP \u5185\u5efa\u7684\u9a8c\u8bc1\u51fd\u6570\uff08\u5982 <code>filter_var()<\/code>\uff09\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u6b63\u786e\u3002<\/li>\n\n\n\n<li><strong>\u9632\u6b62 XSS \u653b\u51fb\uff1a<\/strong> \u4f7f\u7528 <code>htmlspecialchars()<\/code> \u51fd\u6570\u8f6c\u4e49\u7528\u6237\u8f93\u5165\u7684\u7279\u6b8a\u5b57\u7b26\u3002<\/li>\n\n\n\n<li><strong>\u9632\u6b62 SQL \u6ce8\u5165\uff1a<\/strong> \u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\uff08\u5982 <code>PDO<\/code> \u6216 <code>mysqli<\/code>\uff09\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u9a8c\u8bc1\u548c\u6e05\u7406\u8868\u5355\u6570\u636e<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nif ($_SERVER&#91;'REQUEST_METHOD'] == 'POST') {\n    \/\/ \u83b7\u53d6\u5e76\u6e05\u7406\u7528\u6237\u8f93\u5165\n    $name = isset($_POST&#91;'name']) ? htmlspecialchars($_POST&#91;'name']) : '';\n    $age = isset($_POST&#91;'age']) ? (int)$_POST&#91;'age'] : 0;\n\n    \/\/ \u9a8c\u8bc1\u5e74\u9f84\u662f\u5426\u5408\u6cd5\n    if ($age &lt; 18 || $age &gt; 100) {\n        echo \"\u5e74\u9f84\u4e0d\u5408\u6cd5\uff01&lt;br&gt;\";\n    } else {\n        echo \"\u59d3\u540d: \" . $name . \"&lt;br&gt;\";\n        echo \"\u5e74\u9f84: \" . $age . \"&lt;br&gt;\";\n    }\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 <code>htmlspecialchars()<\/code> \u51fd\u6570\u6e05\u7406\u4e86\u7528\u6237\u7684\u8f93\u5165\u6570\u636e\uff0c\u5e76\u9a8c\u8bc1\u4e86\u5e74\u9f84\u7684\u5408\u6cd5\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>5\ufe0f\u20e3 \u63a8\u8350\u9605\u8bfb\u4e0e\u6587\u6863<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/reserved.variables.post.php\">PHP $_POST \u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/function.filter-var.php\">PHP <code>filter_var()<\/code> \u51fd\u6570<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/function.htmlspecialchars.php\">PHP <code>htmlspecialchars()<\/code> \u51fd\u6570<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/features.file-upload.php\">PHP \u6587\u4ef6\u4e0a\u4f20\u5b98\u65b9\u6587\u6863<\/a><\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7 <code>$_POST<\/code>\uff0c\u4f60\u53ef\u4ee5\u5b89\u5168\u3001\u6709\u6548\u5730\u63d0\u4ea4\u8868\u5355\u6570\u636e\uff0c\u7279\u522b\u9002\u7528\u4e8e\u654f\u611f\u6570\u636e\u548c\u5927\u89c4\u6a21\u6570\u636e\u7684\u4f20\u8f93\u3002\u4e3a\u4e86\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\uff0c\u59cb\u7ec8\u4f7f\u7528\u9002\u5f53\u7684\u9a8c\u8bc1\u4e0e\u6e05\u7406\u673a\u5236\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>$_POST \u662f PHP \u8d85\u7ea7\u5168\u5c40\u53d8\u91cf\u4e4b\u4e00\uff0c\u7528\u4e8e\u901a\u8fc7 HTTP POST \u65b9\u6cd5\u5411\u670d\u52a1\u5668\u53d1\u9001\u6570\u636e\u3002\u4e0e $_GET [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3029,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3028","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\/3028","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=3028"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3028\/revisions"}],"predecessor-version":[{"id":3030,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3028\/revisions\/3030"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3029"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}