Apache server variables

Server Variables are variables set by Apache, and held inside the super global array ‘$_SERVER’. They hold very interesting snippits of data available for use, and become very useful when developing PHP projects.


It return the URL in to access the page which is executing the script. If you need to type http://www.example.com/product.php?id=5 to access the page then $_SERVER[‘REQUEST_URI’] returns “/product.php?id=5.


Returns the root directory of the server which is specified in the configuration file of server. This variable usually returns the path like “/usr/yoursite/www” in Linux and “D:/xamps/xampp/htdocs” in windows.


Returns the host’s name as found in the http header. This variable usually returns the path like “example.com” when the you find “http://example.com” in browser’s address-bar and return “www.example.com” when you see http://www.example.com in the address-bar. This is quite useful when you’ve to preserve session while making online payment using PHP since session stored for “http://example.com” is not same as for the “http://www.example.com”.


Returns the user agent’s (browser) detail accessing the web page. We can use strpos($_SERVER[“HTTP_USER_AGENT”],”MSIE”) to detect Microsoft Internet explorer or you can use strpos($_SERVER[“HTTP_USER_AGENT”],”Firefox”) to detect firefox browser in PHP.


Returns the file-name of the currently executing script. Let’s suppose that you’re accessing the URL http://www.example.com/product.php?id=5 then $_SERVER[‘PHP_SELF’] returns “/product.php” in your script.


Returns the query string if query string is used to access the script currently executing. Query strings are those string which is available after “?” sign.if you use $_SERVER[‘QUERY_STRING’] in the script executing the following URL “http://www.example.com/index.php?id=5&page=product” then it returns “id=5&page=product” in your script.


Returns the IP address of remote machine accessing the current page. But you can’t relie on $_SERVER[‘REMOTE_ADDR’] to get the real IP address of client’s machine.


Returns the absolute path of the file which is currently executing. It returns path like “var/example.com/www/product.php” in Linux and path like “D:/xampp/xampp/htdocs/test/example.php” in windows.