Important PHP Variables ($_SERVER Values)

A short list of values that got often used by PHP-developers.


Libraries

Code Snippet
//URL: http://example.com/folder1/folder2/yourfile.php?var=blabla#12345

$_SERVER["DOCUMENT_ROOT"] === /home/user/www
$_SERVER["SERVER_ADDR"]   === 143.34.112.23
$_SERVER['HTTP_HOST']     === example.com (or with WWW)
$_SERVER["REQUEST_URI"]   === /folder1/folder2/yourfile.php?var=blabla
__FILE__                  === /home/user/www/folder1/folder2/yourfile.php  --->//p.s. ON WINDOWS SERVERS, instead of / is \
basename(__FILE__)        === yourfile.php
__DIR__                   === /home/user/www/folder1/folder2 [same: dirname(__FILE__)]
$_SERVER["QUERY_STRING"]  === var=blabla

$_SERVER["REQUEST_URI"]   === /folder1/folder2/yourfile.php?var=blabla
parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)  === /folder1/folder2/yourfile.php 
$_SERVER["PHP_SELF"]      === /folder1/folder2/yourfile.php

//if "parentfile.php" includes this "yourfile.php"(and inside it are the codes written), and "parentfile.php?a=123" is opened, then
$_SERVER["PHP_SELF"]       === /parentfile.php
$_SERVER["REQUEST_URI"]    === /parentfile.php?a=123
$_SERVER["SCRIPT_FILENAME"]=== /home/user/www/parentfile.php
str_replace($_SERVER["DOCUMENT_ROOT"],'', str_replace('\\','/',__FILE__ ) )  === /folder1/folder2/yourfile.php


Implementation

In the list above I used the URL<code>http://example.com/folder1/folder2/yourfile.php?var=blabla#12345</code> Use for example<code>$domain = $_SERVER['HTTP_HOST'];</code> to get the domain from the server the script is hosted on. In example it will return<code>example.com</code>





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 0 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    Basics



    Informations

    posted on 2016-09-21 21:08:05

    viewed 36 times

    snippet's UUID is 57e2da94d6aa1

    Similar Snippets