8 useful server variables available in PHP

Advertisement

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.

Tags:

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

18 Comments on “8 useful server variables available in PHP”

  • Catalog Movies wrote on 14 May, 2008, 2:42

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

  • Roshan wrote on 14 May, 2008, 4:16

    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…

  • tevan wrote on 14 May, 2008, 9:07

    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.

  • JT wrote on 14 May, 2008, 10:27

    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.

  • Roshan wrote on 14 May, 2008, 11:13

    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

  • John wrote on 10 February, 2009, 15:55

    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.

  • Pakistani wrote on 21 February, 2009, 11:22

    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

  • chiladelphia wrote on 13 October, 2009, 18:33

    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

  • goyal wrote on 13 February, 2010, 6:26

    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?

  • Ben wrote on 8 May, 2010, 8:03

    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.

  • Sondra wrote on 16 July, 2010, 23:55

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

  • shaffy wrote on 10 August, 2010, 5:35

    Its really useful,thanks.

  • links london wrote on 22 January, 2011, 9:24

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

Trackbacks

  1. Server variables in PHP | PHP Readings
  2. 8 ??????? ???????????? ????????? ?????????? ? PHP | ????????.??
  3. Daily Find #68 | TechToolBlog
  4. Variables del servidor útiles en PHP : Notitodo
  5. Prevent form post request from another domain in PHP

Write a Comment

 


Copyright © 2014 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.