Using $_SERVER in PHP

A Look at Superglobals in PHP

Smiling businesswoman working at laptop in office
Paul Bradbury/OJO Images/Getty Images

$_SERVER is one of the PHP global variables—termed Superglobals—which contain information about server and execution environments. These are pre-defined variables so they are always accessible from any class, function or file.

The entries here are recognized by web servers, but there is no guarantee that each web server recognizes every Superglobal. These three PHP $_SERVER arrays all behave in similar ways—they return information about the file in use.

When exposed to different scenarios, in some cases they behave differently. These examples may help you decide which is best for what you need. A full list of $_SERVER arrays is available at the PHP website.

$_SERVER['PHP_SELF']

PHP_SELF is the name of the currently executing script.

  • http://www.yoursite.com/example/ -- --> /example/index.php
  • http://www.yoursite.com/example/index.php -- --> /example/index.php
  • http://www.yoursite.com/example/index.php?a=test -- -->  /example/index.php
  • http://www.yoursite.com/example/index.php/dir/test -- -->  /dir/test

When you use $_SERVER[’PHP_SELF’], it returns the file name /example/index.php both with and without the file name typed in the URL. When variables are appended at the end, they were truncated and again /example/index.php was returned. The only version that produced a different result has directories appended after the file name. In that case, it returned those directories.

$_SERVER['REQUEST_URI']

REQUEST_URI refers to the URI given to access a page.

  • http://www.yoursite.com/example/ -- -->  /
  • http://www.yoursite.com/example/index.php -- -->  /example/index.php
  • http://www.yoursite.com/example/index.php?a=test -- -->  /example/index.php?a=test
  • http://www.yoursite.com/example/index.php/dir/test -- -->  /example/index.php/dir/test

    All of these examples,returned exactly what was entered for the URL. It returned a plain /, the file name, the variables, and the appended directories, all just as they were entered.

    $_SERVER['SCRIPT_NAME']

    SCRIPT_NAME is the current script's path. This comes in handy for pages that need to point to themselves.

    • http://www.yoursite.com/example/ -- -->  /example/index.php
    • http://www.yoursite.com/example/index.php -- -->  /example/index.php
    • http://www.yoursite.com/example/index.php?a=test -- -->  /example/index.php
    • http://www.yoursite.com/example/index.php/dir/test -- -->  /example/index.php

    All cases here returned only the file name /example/index.php regardless of whether it was typed, not typed, or anything was appended to it.

    Format
    mla apa chicago
    Your Citation
    Bradley, Angela. "Using $_SERVER in PHP." ThoughtCo, Aug. 20, 2017, thoughtco.com/using-server-in-php-2693940. Bradley, Angela. (2017, August 20). Using $_SERVER in PHP. Retrieved from https://www.thoughtco.com/using-server-in-php-2693940 Bradley, Angela. "Using $_SERVER in PHP." ThoughtCo. https://www.thoughtco.com/using-server-in-php-2693940 (accessed November 20, 2017).