Science, Tech, Math › Computer Science Using $_SERVER in PHP Share Flipboard Email Print Paul Bradbury/OJO Images/Getty Images Computer Science PHP Programming Tutorials MySQL Commands Perl Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Angela Bradley Angela Bradley Computer Science Expert B.A, History, Eastern Oregon University Angela Bradley is a web designer and programming expert with over 15 years of experience. An expert in iOS software design and development, she specializes in building technical hybrid platforms. Learn about our Editorial Process Updated on July 03, 2019 $_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.phphttp://www.yoursite.com/example/index.php -- --> /example/index.phphttp://www.yoursite.com/example/index.php?a=test -- --> /example/index.phphttp://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.phphttp://www.yoursite.com/example/index.php?a=test -- --> /example/index.php?a=testhttp://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.phphttp://www.yoursite.com/example/index.php -- --> /example/index.phphttp://www.yoursite.com/example/index.php?a=test -- --> /example/index.phphttp://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. Cite this Article Format mla apa chicago Your Citation Bradley, Angela. "Using $_SERVER in PHP." ThoughtCo, Aug. 26, 2020, thoughtco.com/using-server-in-php-2693940. Bradley, Angela. (2020, August 26). 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 March 22, 2023). copy citation