{"id":3024,"date":"2025-03-19T22:09:26","date_gmt":"2025-03-19T14:09:26","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3024"},"modified":"2025-03-19T22:09:26","modified_gmt":"2025-03-19T14:09:26","slug":"php-_get-%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/19\/php-_get-%e5%8f%98%e9%87%8f\/","title":{"rendered":"PHP $_GET \u53d8\u91cf"},"content":{"rendered":"\n<p><code>$_GET<\/code> \u662f PHP \u8d85\u7ea7\u5168\u5c40\u53d8\u91cf\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u4e8e\u901a\u8fc7 URL \u5411\u670d\u52a1\u5668\u53d1\u9001\u6570\u636e\u3002\u5b83\u7528\u4e8e\u83b7\u53d6 URL \u4e2d\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\uff08\u67e5\u8be2\u53c2\u6570\uff09\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u5728\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165\u7c7b\u4f3c <code>http:\/\/example.com\/index.php?name=John&amp;age=25<\/code> \u8fd9\u6837\u7684 URL \u65f6\uff0c<code>$_GET<\/code> \u53d8\u91cf\u5c06\u5305\u542b <code>name<\/code> \u548c <code>age<\/code> \u4f5c\u4e3a\u952e\uff0c\u5e76\u5b58\u50a8\u76f8\u5e94\u7684\u503c\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>$_GET<\/code> \u53d8\u91cf\u7684\u57fa\u672c\u7528\u6cd5<\/strong><\/h2>\n\n\n\n<p>\u5f53\u901a\u8fc7 <code>GET<\/code> \u65b9\u6cd5\u63d0\u4ea4\u8868\u5355\u65f6\uff0c\u6570\u636e\u4f1a\u4ee5\u67e5\u8be2\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u9644\u52a0\u5230 URL \u540e\u9762\u3002<code>$_GET<\/code> \u5141\u8bb8\u4f60\u5728 PHP \u811a\u672c\u4e2d\u8bbf\u95ee\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b 1\uff1a\u901a\u8fc7 URL \u83b7\u53d6\u53c2\u6570<\/strong><\/h3>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b URL\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;example.com\/index.php?name=John&amp;age=25<\/code><\/pre>\n\n\n\n<p>\u5728 PHP \u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>$_GET<\/code> \u6765\u8bbf\u95ee URL \u4e2d\u7684\u67e5\u8be2\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u83b7\u53d6 URL \u4e2d\u7684\u67e5\u8be2\u53c2\u6570\n$name = $_GET&#91;'name']; \/\/ John\n$age = $_GET&#91;'age'];   \/\/ 25\n\necho \"\u59d3\u540d: \" . $name . \"&lt;br&gt;\";\necho \"\u5e74\u9f84: \" . $age . \"&lt;br&gt;\";\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2\ufe0f\u20e3 \u4f7f\u7528 <code>$_GET<\/code> \u83b7\u53d6\u8868\u5355\u6570\u636e<\/strong><\/h2>\n\n\n\n<p>\u901a\u8fc7 <code>GET<\/code> \u65b9\u6cd5\u63d0\u4ea4\u7684\u8868\u5355\u4f1a\u5c06\u6570\u636e\u9644\u52a0\u5230 URL \u4e2d\uff0c\u5e76\u4f7f\u7528 <code>$_GET<\/code> \u53d8\u91cf\u8fdb\u884c\u8bbf\u95ee\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>HTML \u8868\u5355<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form action=\"index.php\" method=\"get\"&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<h3 class=\"wp-block-heading\"><strong>PHP \u5904\u7406\u811a\u672c\uff08index.php\uff09<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u83b7\u53d6\u8868\u5355\u6570\u636e\nif (isset($_GET&#91;'name']) &amp;&amp; isset($_GET&#91;'age'])) {\n    $name = $_GET&#91;'name'];\n    $age = $_GET&#91;'age'];\n\n    echo \"\u59d3\u540d: \" . $name . \"&lt;br&gt;\";\n    echo \"\u5e74\u9f84: \" . $age . \"&lt;br&gt;\";\n} else {\n    echo \"\u8bf7\u586b\u5199\u8868\u5355\uff01\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u63d0\u4ea4\u540e\u7684 URL<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;example.com\/index.php?name=John&amp;age=25<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3\ufe0f\u20e3 \u5b89\u5168\u6027\u6ce8\u610f\u4e8b\u9879<\/strong><\/h2>\n\n\n\n<p>\u7531\u4e8e <code>$_GET<\/code> \u6570\u636e\u76f4\u63a5\u66b4\u9732\u5728 URL \u4e2d\uff0c\u5b83\u5bb9\u6613\u88ab\u7be1\u6539\u5e76\u4e14\u53ef\u4ee5\u88ab\u65e5\u5fd7\u8bb0\u5f55\u5668\u3001\u6d4f\u89c8\u5668\u5386\u53f2\u7b49\u6cc4\u9732\u3002\u56e0\u6b64\uff0c\u654f\u611f\u6570\u636e\uff08\u5982\u5bc6\u7801\u6216\u79c1\u4eba\u4fe1\u606f\uff09\u4e0d\u5e94\u901a\u8fc7 <code>GET<\/code> \u65b9\u6cd5\u4f20\u9012\u3002\u5bf9\u4e8e\u654f\u611f\u6570\u636e\uff0c\u5e94\u4f7f\u7528 <code>POST<\/code> \u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8fc7\u6ee4\u4e0e\u9a8c\u8bc1\uff1a<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9a8c\u8bc1\u8f93\u5165\u6570\u636e<\/strong>\uff1a\u4f7f\u7528 <code>filter_var()<\/code> \u51fd\u6570\u9a8c\u8bc1 <code>$_GET<\/code> \u6570\u636e\u7684\u683c\u5f0f\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u6e05\u7406<\/strong>\uff1a\u907f\u514d XSS \u653b\u51fb\uff0c\u4f7f\u7528 <code>htmlspecialchars()<\/code> \u51fd\u6570\u6e05\u7406\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u83b7\u53d6\u5e76\u6e05\u7406\u8f93\u5165\u6570\u636e\n$name = isset($_GET&#91;'name']) ? htmlspecialchars($_GET&#91;'name']) : '';\n$age = isset($_GET&#91;'age']) ? (int)$_GET&#91;'age'] : 0;\n\necho \"\u59d3\u540d: \" . $name . \"&lt;br&gt;\";\necho \"\u5e74\u9f84: \" . $age . \"&lt;br&gt;\";\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4\ufe0f\u20e3 \u4f7f\u7528 <code>$_GET<\/code> \u4f20\u9012\u591a\u4e2a\u53c2\u6570<\/strong><\/h2>\n\n\n\n<p><code>$_GET<\/code> \u53ef\u4ee5\u540c\u65f6\u4f20\u9012\u591a\u4e2a\u53c2\u6570\uff0c\u591a\u4e2a\u53c2\u6570\u901a\u8fc7 <code>&amp;<\/code> \u7b26\u53f7\u5206\u9694\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;example.com\/index.php?name=John&amp;age=25&amp;city=NewYork<\/code><\/pre>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5728 PHP \u4e2d\u8bbf\u95ee\u8fd9\u4e9b\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$name = $_GET&#91;'name'];\n$age = $_GET&#91;'age'];\n$city = $_GET&#91;'city'];\n\necho \"\u59d3\u540d: \" . $name . \"&lt;br&gt;\";\necho \"\u5e74\u9f84: \" . $age . \"&lt;br&gt;\";\necho \"\u57ce\u5e02: \" . $city . \"&lt;br&gt;\";\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>5\ufe0f\u20e3 <code>$_GET<\/code> \u548c <code>$_POST<\/code> \u7684\u533a\u522b<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u70b9<\/th><th><code>$_GET<\/code><\/th><th><code>$_POST<\/code><\/th><\/tr><\/thead><tbody><tr><td>\u6570\u636e\u4f20\u8f93\u65b9\u5f0f<\/td><td>URL \u67e5\u8be2\u5b57\u7b26\u4e32<\/td><td>HTTP \u8bf7\u6c42\u4f53<\/td><\/tr><tr><td>\u6570\u636e\u957f\u5ea6\u9650\u5236<\/td><td>URL \u957f\u5ea6\u9650\u5236\uff08\u5927\u7ea6 2000 \u5b57\u7b26\uff09<\/td><td>\u6ca1\u6709\u660e\u663e\u9650\u5236<\/td><\/tr><tr><td>\u5b89\u5168\u6027<\/td><td>\u6570\u636e\u66b4\u9732\u5728 URL \u4e2d<\/td><td>\u6570\u636e\u4e0d\u76f4\u63a5\u66b4\u9732\u5728 URL \u4e2d<\/td><\/tr><tr><td>\u4f7f\u7528\u573a\u666f<\/td><td>\u67e5\u8be2\u6570\u636e\u3001\u7b80\u5355\u8868\u5355<\/td><td>\u63d0\u4ea4\u654f\u611f\u4fe1\u606f\u3001\u5927\u91cf\u6570\u636e<\/td><\/tr><\/tbody><\/table><\/figure>\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 \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.get.php\">PHP $_GET \u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/function.filter-var.php\">PHP \u8fc7\u6ee4\u5668\u51fd\u6570<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/function.htmlspecialchars.php\">PHP htmlspecialchars() \u51fd\u6570<\/a><\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7 <code>$_GET<\/code> \u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece URL \u83b7\u53d6\u7528\u6237\u63d0\u4ea4\u7684\u6570\u636e\u3002\u9002\u5f53\u7684\u9a8c\u8bc1\u548c\u6570\u636e\u6e05\u7406\u662f\u786e\u4fdd\u5e94\u7528\u5b89\u5168\u7684\u5173\u952e\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>$_GET \u662f PHP \u8d85\u7ea7\u5168\u5c40\u53d8\u91cf\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u4e8e\u901a\u8fc7 URL \u5411\u670d\u52a1\u5668\u53d1\u9001\u6570\u636e\u3002\u5b83\u7528\u4e8e\u83b7\u53d6 URL \u4e2d\u7684\u67e5\u8be2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3026,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3024","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\/3024","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=3024"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3024\/revisions"}],"predecessor-version":[{"id":3027,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3024\/revisions\/3027"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3026"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}