When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Wednesday, November 10, 1999

Accessing HTTP Headers and Environment Variables

One of the commonly used collections of the Request object is the ServerVariables collection. For those unfamiliar with this collection, please read Using the Request.ServerVariables Object, which discusses this collection. (Interesting to note, the Using the Request.ServerVariables Object article was the third ever article written for 4Guys!)

- continued -

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:

telnet www.4guysfromrolla.com 80

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:

User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)
Referer: http://www.go.com

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:


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:


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:


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

                <B>Server Varriable</B>

      <% For Each name In Request.ServerVariables %>
                <%= name %>
                <%= Request.ServerVariables(name) %>
      <% Next %>

Happy Programming!

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article