$_GET

$_GETHTTP GET 变量

说明

通过 URL 参数(又叫 query string)传递给当前脚本的变量的数组。 注意:该数组不仅仅对 method 为 GET 的请求生效,而是会针对所有带 query string 的请求。

示例

示例 #1 $_GET 范例

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

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

以上示例的输出类似于:

Hello Hannes!

注释

注意:

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

注意:

GET 是通过 urldecode() 传递的。

add a note add a note

User Contributed Notes 2 notes

up
4
CleverUser123
2 years ago
If you're tired of typing $var = $_GET['var'] to get variables, don't forget that you can also use:

extract($_GET, EXTR_PREFIX_ALL, "g")

So if you have $_GET['under'], you can do $g_under. It's way shorter if you have more get elements! If you don't want prefix, do

extract($_GET)

to get normal. So $_GET['under'] would become $under. Might not extract under if it already exists, however.
up
3
An Anonymous User
3 years ago
<?php
// It is important to sanitize
// input! Otherwise, a bad actor
// could enter '<script src="evilscript.js"></script>'
// in a URL parameter. Assuming you echo it, this
// would inject scripts in an XSS attack.
//
// The solution:
$NAME = $_GET['NAME'];
// Bad:
echo $NAME;
// that one is vulnerable to XSS
// Good:
echo htmlspecialchars($NAME);
// Sanitizes input thoroughly.
?>
To Top