PHP Velho Oeste 2024

$_GET

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

$_GETHTTP GET-Variablen

Beschreibung

Ein assoziatives Array von Variablen, die dem aktuellen Skript mittels der URL-Parameter (dem Query-String) übergeben werden. Es ist zu beachten, dass das Array nicht nur für GET-Anfragen gefüllt wird, sondern für alle Anfragen mit einem Query-String.

Beispiele

Beispiel #1 $_GET-Beispiel

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

Angenommen, der Benutzer gab ein http://example.com/?name=Hannes

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

Hallo Hannes!

Anmerkungen

Hinweis:

Dies ist eine 'Superglobale' oder automatisch globale Variable. Dies bedeutet, dass sie innerhalb des Skripts in jedem Geltungsbereich sichtbar ist. Es ist nicht nötig, sie mit global $variable bekannt zu machen, um aus Funktionen oder Methoden darauf zuzugreifen.

Hinweis:

Die GET-Variablen werden automatisch mit urldecode() behandelt.

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