The Request.ServerVariables collection contains a number of HTTP Headers and environment variables. It is important to understand that these are two entirely different things. The HTTP Headers are additional information the browser sends to the web server when requesting an web page, while the environment variables contain site-wide information stored by the web server. In this article we will examine first the HTTP headers and then the environment variables.

HTTP Headers:
Whenever you visit a web page, a complicated dialog occurs between the client (your browser) and the server (the web server that contains the web page you are requesting). This dialog looks something like this:

* Client: Connect to Server
* Server: Accept connection
* Client: Send HTTP Headers, finishing with a request for the web page
* Server: Log these HTTP headers, retrieve the web page and send it to the client
* Client: disconnect from the server and render the HTML received

In fact, you can actively participate in this dialog if you have access to a UNIX machine. Logon, and enter the following command at the prompt:

This will connect to the 4GuysFromRolla.com web server on port 80 (the default HTTP port). Once you connect to the server, the server waits for you to make your request. Begin by entering in the HTTP headers you want to send. There are several common HTTP headers, the two most common ones being User-Agent and Referer. User-Agent contains information about the browser while Referer, if it exists, indicates the URL that contained the link that the user clicked on to reach the given page. To send the User-Agent and Referer headers, we would type in:

Next, we would need to specify the web page we were interested in retrieving. If we want to obtain the default document, we can do:

GET /

Now, hit enter twice, and you'll see the HTML for http://www.4GuysFromRolla.com/index.shtml! Neat, eh? This is the process that your browser goes through each time you visit a web page! Hopefully this gives you a bit more appreciation for what a web browser needs to do to view a web page! Not only does this dialog need to occur, but a complex parsing algorithm needs to be used to convert the HTML into those pretty animated GIFs and Frames we all love...

As I mentioned, the two most common HTTP Headers are Referer and User-Agent. An HTTP header can be obtained from the Request.ServerVariables collection using the following syntax:

Request.ServerVariables("HTTP_HTTP Header Name")

So, if you want to display the Referer HTTP Header on an ASP page, you can do:

<%=Request.ServerVariables("HTTP_Referer")%>

For a good article on a practical use of the Referer HTTP Header, check out Joao Vieira's Two Ways to Protect your ASP Pages. (Note: Cookies are also pass from the client to the server through HTTP headers!)

Environment Variables
Along with the HTTP Headers, the Request.ServerVariables collection contains a number of site-wide environment variables. These environment variables contain information like the web server software being used (Server_Software), what HTTP port is being used (Server_Port), the URL of the currently processing ASP page (Script_Name), the physical path of the root web application (Appl_Physical_Path), and others. To reference an environment variable from the Request.ServerVariables collection, use the following syntax:

Request.ServerVariables("Environment Variable Name")

So, to list the IP Address of the client requesting a web page, you can use:

<%=Request.ServerVariables("REMOTE_ADDR")%>

In fact, you can list all of the environment variables and HTTP headers using the following code: