Ajax Bible
PHP has a special array built in called $_SERVER, which contains a lot of useful information, such as $_SERVER['PHP_SELF'], which holds the name of the current script, and $_SERVER['REQUEST_METHOD'], which holds the request method that was used (GET, POST, and so on).
The most useful server variables available in $_SERVER are listed in Table 14.1.
| Server Variable | Description |
|---|---|
| AUTH_TYPE | When operating under Apache and using authenticated HTTP this variable holds the authentication type. |
| DOCUMENT_ROOT | Contains the document root directory under which the script is executing. |
| GATEWAY_INTERFACE | Contains the revision of the CGI specification the server is using. |
| HTTP_ACCEPT | Contains the text in the Accept: header from the current request. |
| HTTP_ACCEPT_CHARSET | Contains the text in the Accept-Charset: header from the current request. |
| HTTP_ACCEPT_ENCODING | Contains the text in the Accept-Encoding: header from the current request. |
| HTTP_ACCEPT_LANGUAGE | Contains the text in the Accept-Language: header from the current request. |
| HTTP_CONNECTION | Contains the text in the Connection: header from the current request. |
| HTTP_HOST | Contains the text in the Host: header from the current request. |
| HTTP_REFERER | Contains the address of the page (if any) that referred the user agent to the current page. Set by the browser. |
| HTTP_USER_AGENT | Contains the text in the User-Agent: header from the current request. |
| PATH_TRANSLATED | Specifies the file system-based path to the script. |
| PHP_AUTH_PW | When running under Apache using HTTP authentication, this variable holds the password the user provides. |
| PHP_AUTH_USER | When running under Apache using HTTP authentication, this variable holds the username the user provides. |
| PHP_SELF | Contains the filename of the currently executing script, relative to the document root. |
| QUERY_STRING | Contains the query string, if there was any. |
| REMOTE_ADDR | Contains the IP address from which the user is viewing the current page. |
| REMOTE_HOST | Contains the Host name from which the user is viewing the current page. |
| REMOTE_PORT | Contains the port being used on the user’s machine to communicate with the Web server. |
| REQUEST_METHOD | Specifies which request method was used to access the page; such as GET, HEAD, POST, PUT. |
| REQUEST_URI | The URL that was given in order to access this page, such as /index.html. |
| SCRIPT_FILENAME | The absolute pathname of the currently executing script. |
| SCRIPT_NAME | Contains the current script’s path. This is useful for pages that need to point to themselves. |
| SERVER_ADMIN | Contains the value given to the SERVER_ADMIN directive in the Web server configuration file. |
| SERVER_NAME | Contains the name of the server host under which the script is executing. |
| SERVER_PORT | Contains the port on the server machine being used by the Web server for communication. |
| SERVER_PROTOCOL | Contains the name and revision of the information protocol by which the page was requested. |
| SERVER_SIGNATURE | Contains the server version and virtual host name. |
| SERVER_SOFTWARE | Contains the server identification string. |
Say you wanted to determine the type of browser the user has; you might want to use a <marquee> element, for example, which is supported only by Microsoft Internet Explorer. You could start with this page, browser.html:
<html> <head> <title> Finding browser type </title> </head> <body> <center> <h1>Finding browser type</h1> <form method="post" action="browser.php"> <input type="submit" value="Submit"> </form> </center> </body> </html>
In browser.php, you can check whether you’re dealing with Internet Explorer by using the PHP strpos function to search $_SERVER["HTTP_USER_AGENT"] for the text MSIE, which that string contains if you’re using Internet Explorer:
<html> <head> <title>Finding browser type</title> </head> <body> <center> <h1>Finding browser type</h1> <br> <? if(strpos($_SERVER["HTTP_USER_AGENT"], "MSIE")){ . . . } ?> </center> </body> </html>
If you are using Internet Explorer, you can display a <marquee> element; otherwise, you might just display a message:
<html> <head> <title>Finding browser type</title> </head> <body> <center> <h1>Finding browser type</h1> <br> <? if(strpos($_SERVER["HTTP_USER_AGENT"], "MSIE")){ echo("<marquee><h1>Welcome to my page!</h1></marquee>"); } else { echo("<h1>Please get Internet Explorer</h1>"); } ?> </center> </body> </html>
The browser.html page is shown in Figure 14.3.
When you click the Submit button, you see the marquee if you’re using Internet Explorer, as shown in Figure 14.4.
You can also group the data from a form into an array for easier handling in PHP, which is discussed in the next section.