What are the PHP superglobal variables

Super global variables (Theme: PHP examples)

1. Explanations

Superglobals are variables that can be accessed from anywhere in the script at any time. They are therefore in the global scope and the use of the keyword “global” or access via $ GLOBALS is not necessary. Probably the most important superglobal variables are $ _GET and $ _POST, which contain data sent by the user. In detail, there are the following noteworthy superglobals (some more still exist, but they are of little importance or are already out of date):

  • $ _GET: All request parameters from the URL. For example, at the URL http://www.example.com/page.php?foo=bar&test=123 the GET parameters are “foo” (with the value “bar”) and “test” (with the value “123”)
  • $ _POST: All parameters sent via HTTP POST. Usually these are sent by the user as soon as he submits a completed form with the attribute “method” set to “post”.
  • $ GLOBALS: All global variables defined in the script are placed in this array.
  • $ _SERVER: All data generated by the web server, such as the URL accessed, the user's IP or file paths.
  • $ _COOKIE: Cookies sent by the visitor.
  • $ _SESSION: The data of the visitor's session. A session is used to save data across multiple page views.
  • $ _FILES: files sent by the visitor.

2nd example: $ _GET

Assuming that a file test.php exists and this is called with test.php? Foo = bar & test = 123 & example:

PHP code <?php // Datei aufgerufen via test.php?foo=bar&test=123&beispiel // Ausgeben aller GET-Parameter var_dump($_GET); // Auf einzelne Parameter kann ebenfalls zugegriffen werden echo('foo: ' . $_GET['foo'] . "\n"); echo('test: ' . $_GET['test']); ?>
HTML code: output array (3) {["foo"] => string (3) "bar" ["test"] => string (3) "123" ["example"] => string (0) ""} foo: bar test: 123

3. Example: $ _POST

The following example shows a short form that sends its data via POST. After submitting the form, the content of the input field is displayed.

PHP code <form action="" method="post"> <input type="text" name="foo" value="bar" /> <input type="submit" /> </form> <?php if (isset($_POST['foo'])) { echo('foo: ' . $_POST['foo'] . "\n"); } ?>

Output before sending the form:

HTML code: output <form action="" method="post"> <input type="text" name="foo" value="bar" /> <input type="submit" /> </form>

Output after submitting the form (provided the input field remains unchanged on "bar"):

HTML code: output
foo: bar

4. Example: $ GLOBALS

In this example, a variable is written to the $ GLOBALS array and called up by the echoMyGlobal () function, which would normally not have access to this variable if it were not in the said array.

PHP code <?php $GLOBALS['myGlobal'] = 'beispiel'; function echoMyGlobal() { echo('myGlobal: ' . $GLOBALS['myGlobal'] . "\n"); } echoMyGlobal(); ?>
HTML code: output myGlobal: example

5. Example: $ _SERVER

In this example, the superglobal variable $ _SERVER is used to determine information about the IP address of the caller, the call method and the current PHP file.

PHP code <?php var_dump($_SERVER['REMOTE_ADDR']); var_dump($_SERVER['REQUEST_METHOD']); var_dump($_SERVER['PHP_SELF']); ?>
HTML code: output string (9) "" string (3) "GET" string (9) "/test.php"

6. Example: $ _COOKIE

A cookie is set using setcookie (), which in turn is read using $ _COOKIE (a super global). The cookie is set the first time the page is accessed and can therefore not yet be read from $ _COOKIE. It is available the second time it is accessed (provided that the visitor has activated cookies).

PHP code <?php setcookie('mycookie', 'some_value'); if (isset($_COOKIE['mycookie'])) { echo('mycookie: ' . $_COOKIE['mycookie']); } else { echo('mycookie nicht gefunden.'); } ?>

Output when the page is viewed for the first time:

HTML code: output mycookie not found.

Output when the page is viewed the second time:

HTML code: output mycookie: some_value

7. Example: $ _SESSION

First, a $ _SESSION is started using the session_start () function. This is absolutely necessary, otherwise the information in $ _SESSION will be lost if a session has not yet been started or will not be reloaded if a session has recently been started for the visitor. After starting / loading the session, a check is made to see whether the "time" value has already been set. If not, it will be set to the current time. Otherwise it will be issued. When the page is called up for the first time, you only get the message that the time has been set. The time is only displayed from the second call, which remains constant for all subsequent calls. (If you simply reload the page, you must have activated cookies.)

PHP code <?php session_start(); if (!isset($_SESSION['time'])) { $_SESSION['time'] = date('H:i:s'); echo('Zeit gesetzt.'); } else { var_dump($_SESSION['time']); } ?>

Output on first call:

HTML code: output Time set.

Output on the second call:

HTML code: output string (8) "19:52:47"