8 useful server variables available in PHP

Posted on May 13, 2008 
Filed Under php, tutorial

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.

Popularity: 27% [?]

If you like this post then please subscribe to my full RSS feed . You can also subscribe by email and have new posts sent directly to your inbox.And, You can also follow me on twitter at http://twitter.com/roshanbh.

Related Posts

» Prevent form post request from another domain in PHP
» Register Globals ( register_globals ) “on” security problem in PHP
» Password protect a page using HTTP Authentication in PHP
» Solving time difference between hosting server and local timezone in PHP

Comments

10 Responses to “8 useful server variables available in PHP”

  1. Server variables in PHP | PHP Readings on May 13th, 2008 11:26 am

    [...] found this great article for PHP beginners. In server variables you could find very useful information about server [...]

  2. Catalog Movies on May 14th, 2008 2:42 am

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

  3. Roshan on May 14th, 2008 4:16 am

    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…

  4. tevan on May 14th, 2008 9:07 am

    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.

  5. JT on May 14th, 2008 10:27 am

    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.

  6. Roshan on May 14th, 2008 11:13 am

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

    http://roshanbh.com.np/2008/01/absolute-path-and-relative-path-file-inclusion-in-php.html

  7. 8 ??????? ???????????? ????????? ?????????? ? PHP | ????????.?? on May 14th, 2008 11:20 pm

    [...] ????????: ???? ?????? ????????. [...]

  8. Daily Find #68 | TechToolBlog on May 15th, 2008 12:50 pm

    [...] 8 useful server variables available in PHP - If you do PHP you probably already know these. [...]

  9. Variables del servidor Ăștiles en PHP : Notitodo on May 16th, 2008 11:24 am

    [...] 8 useful server variables available in PHP [...]

  10. Prevent form post request from another domain in PHP on June 22nd, 2008 9:54 am

    [...] time, I’ve posted the article about useful server variables in PHP. Among them, we can use HTTP_REFERRER server variables to prevent the cross domain form post [...]

Leave a Reply