Superglobals are built-in variables in PHP that are always accessible from any script, function, or class, without having to use the global keyword. They are special associative arrays used to store information about the environment, user input, session state, and server paths.
There are nine PHP Superglobal variables. We have already encountered four of them ($_GET, $_POST, $_SESSION, $_COOKIE), so this chapter will focus on the remaining crucial ones.
1. $_SERVER (Server Environment Information)
The $_SERVER array holds information about the server and execution environment, such as headers, paths, and script locations. It is essential for security checks, routing, and conditional page loading.
| Key Example | Description | Purpose |
$_SERVER['REQUEST_METHOD'] | The method used to access the page (e.g., "POST" or "GET"). | Crucial for form validation. |
$_SERVER['PHP_SELF'] | The filename of the currently executing script. | Used to define the action attribute in HTML forms for submission to the same page. |
$_SERVER['HTTP_HOST'] | The host header used by the client (e.g., "www.example.com"). | Used to build absolute URLs. |
$_SERVER['QUERY_STRING'] | The query string (everything after the ? in the URL). | Used for analysis and custom routing. |
$_SERVER['REMOTE_ADDR'] | The IP address of the user viewing the current page. | Used for logging, analytics, or blocking malicious users. |
Example: Self-Submitting Form Action
<?php
// The form submits data back to the same script file
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
// ... input fields ...
echo '</form>';
?>
2. $_FILES (Handling Uploaded Files)
The $_FILES array is used when a user uploads a file via an HTML form. This array is populated only if the form has the attribute enctype="multipart/form-data".
It stores temporary information about the uploaded file before it is moved to its permanent location.
| Key | Description | Example Value |
$_FILES['field_name']['name'] | The original name of the file on the user’s computer. | "document.pdf" |
$_FILES['field_name']['tmp_name'] | The temporary path where the file is stored on the server. | "/tmp/phpXyZ123" |
$_FILES['field_name']['size'] | The size of the file in bytes. | 51200 |
$_FILES['field_name']['error'] | The error code associated with the upload (0 means success). | 0 |
Example (Preview):
<?php
// Assume form field name is "user_file"
if ($_FILES['user_file']['error'] === UPLOAD_ERR_OK) {
echo "File " . $_FILES['user_file']['name'] . " uploaded successfully to temp location.";
// You would now use move_uploaded_file() to save it permanently
}
?>
Note: Handling file uploads is complex and requires its own dedicated chapter for security and proper implementation.
3. $_ENV (Environment Variables)
The $_ENV array holds variables passed to the script from the environment under which the PHP parser is running. This is commonly used to access system-level settings, such as paths or crucial configuration data like API keys (though using a dedicated configuration file is often preferred).
Example:
<?php
// This value would be set in the server's configuration (e.g., Apache or Nginx)
echo "Server Software: " . $_ENV['SERVER_SOFTWARE'];
?>
4. Summary of All 9 Superglobals
For completeness, here is the full list of PHP Superglobals:
| Superglobal | Purpose | Covered In |
$GLOBALS | Contains a reference to all variables defined in the global scope. | – |
$_SERVER | Server and execution environment information. | This chapter. |
$_GET | Variables passed via the URL query string. | Forms chapter. |
$_POST | Variables passed via the HTTP POST method. | Forms chapter. |
$_FILES | Variables related to file uploads. | This chapter. |
$_COOKIE | Variables passed via HTTP cookies. | Sessions/Cookies chapter. |
$_SESSION | Variables registered to the current session. | Sessions/Cookies chapter. |
$_REQUEST | Contains the contents of $_GET, $_POST, and $_COOKIE. | – |
$_ENV | Variables passed to the script from the environment. | This chapter. |
