Server Environment Variables


Now let's look at the information that PHP allows you to find out from your web server.

The $_SERVER super-global array contains a number of elements that give information about the web server environment during the current page request. To see the full list within the context of a script, you execute this statement at any time:

 print_r($_SERVER); 

The examples in this section are common to most web servers. However, some servers may not support all the values shown or may use different names. You can always refer to the output from the previous statement to check which values are available in your script.

Script Information

The name of the current script can be found in $_SERVER["SCRIPT_NAME"]. Knowing this name can be useful if you want to create a form that submits to itself but whose filename you might want to change in the future. You could use the following tag:

 <FORM ACTION="<?php print $_SERVER["SCRIPT_NAME"];?>"        METHOD=POST> 

Similar to SCRIPT_NAME is the REQUEST_URI element, which contains the full uniform resource identifier of the page request. This consists of the full path to the current script, including the question mark and values in the query string, if there are any. The query string is not included as part of the SCRIPT_NAME element, but you can access it on its own as $_SERVER["QUERY_STRING"].

If you want to find the domain name under which a script is running, you can look at $_SERVER["HTTP_HOST"]. Your web server might be set up with several alias domains, and this provides a way to see which domain name a visitor is viewing your pages on.

User Information

The HTTP_USER_AGENT element contains a string that identifies the user's web browser software and operating system. It might look like one of the following:

 Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5)               Gecko/20041107 Firefox/1.0 Lynx/2.8.5dev.7 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7a 

These three examples correspond to Internet Explorer, Mozilla Firefox, and Lynx, respectively. Notice that both Internet Explorer and Firefox report themselves as Mozilla browsers, so to find out specifically which program a user has, you have to look further into the string.

The following condition can be used to produce different output for Internet Explorer than for Firefox:

 if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {   echo "You are using Internet Explorer"; } elseif (strstr($_SERVER["HTTP_USER_AGENT"], "Firefox")) {   echo "You are using Firefox"; } else {   echo "You are using some other web browser"; } 

You may need to use this occasionally because of differences in the browsers' implementations of DHTML or JavaScript.

The REMOTE_ADDR attribute contains the IP address that the hit to the web server came from. It should either be the IP address of the user's computer or the user's ISP's web cache. You might want to use the remote IP address for logging or security.

If the REMOTE_ADDR value is a web cache, the element HTTP_X_FORWARDED_FOR is also present, and it contains the IP address of the user's computer.

If the user has logged in by using basic HTTP authentication, you can also find out his or her username by looking at the value in $_SESSION["REMOTE_USER"].

Server Information

Other elements in $_SERVER allow you to access various values related to the web server configuration.

For instance, $_SERVER["SERVER_NAME"] corresponds to the ServerName Apache directive. This is the primary name of this web server, but not necessarily the domain name the scripts are being accessed from; it might not be the same as $_SERVER["HTTP_HOST"]. Similarly, $_SESSION["SERVER_ADMIN"] holds the webmaster's email address that is set in the ServerAdmin directive.

The SERVER_ADDR and SERVER_PORT elements contain the IP address and port number of the machine the web server is running on. Checking $_SERVER["REQUEST_METHOD"] reveals whether a GET or POST method was used to pass values to the script.

Finally, if you are working on a shared web host or someone else's web server and want to see what web server software version that person is running, you can check $_SERVER["SERVER_SOFTWARE"]. This value is the same as the one transmitted in the Server header at the beginning of this lesson, and it is similar to the following:

 Apache/1.3.29 (Unix) mod_gzip/1.3.26.1a PHP/4.3.9  mod_ssl/2.8.16 OpenSSL/0.9.7c 



    Sams Teach Yourself PHP in 10 Minutes
    Sams Teach Yourself PHP in 10 Minutes
    ISBN: 0672327627
    EAN: 2147483647
    Year: 2005
    Pages: 151
    Authors: Chris Newman

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net