$_GET

$HTTP_GET_VARS [已弃用]

$_GET -- $HTTP_GET_VARS [已弃用]HTTP GET 变量

说明

通过 URL 参数传递给当前脚本的变量的数组。

$HTTP_GET_VARS 包含相同的信息, 但它不是一个超全局变量。 (注意 $HTTP_GET_VARS$_GET 是不同的变量,PHP 处理它们的方式不同)

更新日志

版本 说明
4.1.0 引入 $_GET,弃用 $HTTP_GET_VARS

范例

Example #1 $_GET 范例

<?php
echo 'Hello ' htmlspecialchars($_GET["name"]) . '!';
?>

假设用户访问的是 http://example.com/?name=Hannes

以上例程的输出类似于:

Hello Hannes!

注释

Note:

"Superglobal"也称为自动化的全局变量。这就表示其在脚本的所有作用域中都是可用的。不需要在函数或方法中用 global $variable; 来访问它。

Note:

GET 是通过 urldecode() 传递的。

User Contributed Notes

php at securetech dot com dot au 16-Jan-2020 12:22
When using $_GET, please consider the security implications of this, as an attacker can post whatever they want, which gets included into your code, unless you are careful and sanitize it and check for VALID values, don't just use whatever is returned.

Instead of using this, i would recommend a function, which will return a sanitized version of the $_GET['variable']

I personally have a function _GET($par, $parType = '')

this means i can swap this into the code for $_GET['variable'] such as _GET('variable')

the function then checks if the second (optional) parameter has been checked:
if($parType == '')
{
    $parType = gettype($par);
}

next, we need to sanitize the string, to ensure no really bad stuff can happen. Check the type to ensure we filter correct type of data, for example if type is 'email' then:
$return = filter_input(INPUT_GET, $par, FILTER_SANITIZE_EMAIL)
or
case 'int':
$return = filter_input(INPUT_GET, $par, FILTER_SANITIZE_NUMBER_INT);

you can read some good security information on OWASP, but it isn't targetted to PHP.
whatchildisthis 27-Aug-2018 01:58
The variable name $_GET is a bit misleading. It works with any HTTP request method that has a query component in the URI: GET, POST, PUT, PATCH, DELETE. A better name would be $_QUERY, similar to http_build_query and PHP_URL_QUERY in parse_url.
chris at bjelleklang dot org 18-Dec-2010 02:40
Please note that PHP setups with the suhosin patch installed will have a default limit of 512 characters for get parameters. Although bad practice, most browsers (including IE) supports URLs up to around 2000 characters, while Apache has a default of 8000.

To add support for long parameters with suhosin, add
suhosin.get.max_value_length = <limit> in php.ini
John Galt 14-Jun-2010 05:57
Just a note, because I didn't know for sure until I tested it.

If you have a query string that contains a parameter but no value (not even an equals sign), like so:
http://path/to/script.php?a

The following script is a good test to determine how a is valued:
<pre>
<?php
 print_r
($_GET);
 if(
$_GET["a"] === "") echo "a is an empty string\n";
 if(
$_GET["a"] === false) echo "a is false\n";
 if(
$_GET["a"] === null) echo "a is null\n";
 if(isset(
$_GET["a"])) echo "a is set\n";
 if(!empty(
$_GET["a"])) echo "a is not empty";
?>
</pre>

I tested this with script.php?a, and it returned:

a is an empty string
a is set

So note that a parameter with no value associated with, even without an equals sign, is considered to be an empty string (""), isset() returns true for it, and it is considered empty, but not false or null. Seems obvious after the first test, but I just had to make sure.

Of course, if I do not include it in my browser query, the script returns
Array
(
)
a is null
timberspine _AT_ gmail _DOT_ com 14-May-2008 04:38
Note that named anchors are not part of the query string and are never submitted by the browser to the server.

Eg.
http://www.xyz-abc.kz/index.php?title=apocalypse.php#doom

echo $_GET['title'];

// returns "apocalypse.php" and NOT "apocalypse.php#doom"

you would be better off treating the named anchor as another query string variable like so:
http://www.xyz-abc.kz/index.php?title=apocalypse.php&na=doom

...and then retrieve it using something like this:
$url = $_GET['title']."#".$_GET['na'];

Hope this helps someone...