略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: $_GET

2024-11-23

$_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 noteadd a note

User Contributed Notes 2 notes

up
4
CleverUser123
6 months 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
1 year 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.
?>

官方地址:https://www.php.net/manual/en/reserved.variables.get.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3