PHP Velho Oeste 2024

$_GET

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

$_GETVariáveis HTTP GET

Descrição

Um array associativo de variáveis passadas para o script atual via os parâmetros da URL (também conhecidos como query string). Note que o array não é preenchido apenas para solicitações GET, mas também para todas requisições com uma query string.

Exemplos

Exemplo #1 Exemplo da $_GET

<?php
echo 'Olá ' . htmlspecialchars($_GET["nome"]) . '!';
?>

Assumindo que o usuário entrou por http://exemplo.com.br/?nome=Hannes

O exemplo acima produzirá algo semelhante a:

Olá Hannes!

Notas

Nota:

Esta é uma variável 'superglobal' ou variável global automática. Isso significa simplesmente que ela está disponível em todos os escopos de um script. Não há necessidade de usar global $variable; para acessá-la dentro de funções ou métodos.

Nota:

Os valores da variável GET são passados pela função 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
2 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