8 useful server variables available in PHP

You guys must have know about server variables in PHP. Server Variables are those variables which are inside the super global array named $_SERVER available in PHP. There are many server variables in PHP and some of them are very useful for fore developing PHP projects. I’m going to post here some of the very useful server variables available in PHP development.

8 Useful server variables available in PHP

1) $_SERVER[‘REQUEST_URI’] – 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”.

2) $_SERVER[‘DOCUMENT_ROOT’] – 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.

3) $_SERVER[‘HTTP_HOST’] – 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”.

4) $_SERVER[‘HTTP_USER_AGENT’] – 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.

5) $_SERVER[‘PHP_SELF’] – 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.

6) $_SERVER[‘QUERY_STRING’] – 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.

7) $_SERVER[‘REMOTE_ADDR’] – 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. See this article to know how to get real IP addrees in PHP.

8 ) $_SERVER[‘SCRIPT_FILENAME’] – 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.

18 thoughts on “8 useful server variables available in PHP

  1. Keep in mind all of these variables should have validation. As they can be manipulated and cause havoc.

  2. I know some of these variables can be tampered and altered by faking http headers and you must take care of those things in your code…

  3. I’m not sure about the other variables listed, but I think $_SERVER[‘REMOTE_ADDR’] is an exception in terms of its susceptibility to dangerous manipulation. the IP address of the remote machine can be spoofed, but it will always be in IP format, and therefore it’s safe to store the REMOTE_ADDR variable as-is in your database. that said, it wouldn’t hurt to validate the REMOTE_ADDR variable first, but that may not be necessary.

  4. JT

    DOCUMENT_ROOT is evil. Depending on server configuration, this might have a trailing slash, or it might not. So, you might write a whole site on the basis that the trailing slash doesn’t exist, only to find that it does. I find it’s more effective from a portability perspective to create a globally accessible configuration constant which holds the document root path to avoid this.

    REQUEST_URI suffers similarly. Again, my solution is a global config constant.

    I would also install a browscap.ini and use get_browser() instead of HTTP_USER_AGENT.

    Alternatively to SCRIPT_FILENAME, PHP magic constants __DIR__ and __FILE__ are very useful.

  5. I know document_root sucks in shared hosting…you can know more about these from here as well..


  6. John

    Here’s a tip.

    Write for the user instead of the search engines. All that first paragraph said to me was ‘php, server variables, server, php, variable, server’. For Christ’s sake we know what you’re talking about, PHP & Server Variables.

  7. I studied the all 8 variables. but i need to track the previous page so dynamically i should know the root. thanks you can help me on mjaved.khan@gmail.com

  8. echo $_SERVER[“SERVER_NAME”].$_SERVER[“REQUEST_URI”] doesn’t return any javascript variables that i’ve tacked on… bummer.

    any idea how i can get the #tabview=tab1

  9. i am making a website, which will translate webpages in different lanaguages. the url of the served page will be as follows: http://www.bbgoyal.com/hi/index.php. in the above example, i dont wish to captute http://www.bbgoyal.com/hi/. tell me how to capture the remaining part?

  10. Ben

    Goyal, use the php function substr to remove everything before the file name or use strpos. There are really a million solutions. Try checking these out on php.net. It’s an epic resource.

  11. Wonderful!! Thank you, thank you! I was able to modify with some little snipps. Added to my fav bookmarks!

  12. shaffy

    Its really useful,thanks.

  13. Great post. Thanks for sharing.I am constantly searching online for articles that can aid me. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *