Skip to content

$_SERVER

Understanding PHP Superglobals and the $_SERVER Variable

PHP is a popular server-side scripting language used for web development. One of its unique features is the use of superglobals, which are special variables that are always accessible, regardless of scope. In this article, we will dive into one of these superglobals, the $_SERVER variable, and explore its properties and uses.

What is the $_SERVER Variable?

The $_SERVER variable is an array that contains information about the server and environment in which the current script is being executed. It includes information such as headers, paths, and script locations, as well as system environment variables. The following are some of the commonly used properties of the $_SERVER variable:

  • `$_SERVER['HTTP_HOST']`: returns the hostname and port number of the server.
  • `$_SERVER['REQUEST_URI']`: returns the requested URL.
  • `$_SERVER['QUERY_STRING']`: returns the query string part of the URL.
  • `$_SERVER['HTTP_REFERER']`: returns the URL of the page that referred the current script.
  • `$_SERVER['REQUEST_METHOD']`: returns the request method (GET, POST, etc.).
  • `$_SERVER['REMOTE_ADDR']`: returns the IP address of the user.
  • `$_SERVER['SCRIPT_FILENAME']`: returns the absolute path to the currently executing script.

Using $_SERVER for URL Redirection

One common use of the $_SERVER variable is for URL redirection. For example, you can use the following code to redirect the user to a different page:

PHP redirection with header function

php
<?php
header('Location: https://www.example.com/');
exit;
?>

In this example, the header function is used to send a raw HTTP header to the browser, telling it to redirect to the specified URL. The exit statement is then used to halt the execution of the script, preventing any further output from being sent to the browser.

Using $_SERVER for Error Handling

Another use of the $_SERVER variable is for error handling. For example, you can use the following code to display a custom error page for 404 errors:

PHP 404 error handling with $_SERVER

php
<?php
// Log the error URI using $_SERVER
error_log('404 Error: ' . $_SERVER['REQUEST_URI']);
http_response_code(404);
include '404.html';
?>

In this example, $_SERVER['REQUEST_URI'] is used to capture the requested path for logging purposes. Instead of redirecting, the script sets the correct HTTP status code and includes a custom template, which is the standard practice for handling errors.

Conclusion

In conclusion, the $_SERVER variable is a powerful tool for web developers, allowing them to access important information about the server and environment in which their scripts are being executed. Whether you're using it for URL redirection, error handling, or other purposes, understanding and utilizing the properties of the $_SERVER variable can greatly improve the functionality and reliability of your PHP scripts.

Note: In modern PHP, the closing ?> tag is optional and often omitted to prevent accidental whitespace output.

Practice

What information can PHP $_SERVER superglobal variable provide?

Dual-run preview — compare with live Symfony routes.