Monday, October 02, 2006

Using Request.ServerVariables in ASP

When a visitor requests a web page on your site, IIS gathers information from both the client computer and the server. This information is stored in a collection known as the Request.ServerVariables collection. ASP code has access to the collection, the information of which can be handy.

With the ServerVariables, you can get the visitor's browser information, the referring URL, the visitor's IP address, the URL being fetched, and a bunch of other information.

Here's a list of some of the more commonly used ones:

  • HTTP_USER_AGENT - the browser the visitor is using
  • LOGON_USER The Windows NT account that the user is logged into.
  • PATH_INFO Extra path information as given by the client. You can access scripts by using their virtual path and the PATH_INFO server variable. If this information comes from a URL it is decoded by the server before it is passed to the CGI script.
  • QUERY_STRING Query information stored in the string following the question mark (?) in the HTTP request.
  • REMOTE_ADDR - The IP address of the remote host making the request.
  • REMOTE_HOST - The name of the host making the request. If the server does not have this information, it will set REMOTE_ADDR and leave this empty.
  • SCRIPT_NAME - A virtual path to the script being executed. This is used for self-referencing URLs.
  • SERVER_NAME - The server's host name, DNS alias, or IP address as it would appear in self-referencing URLs.
  • URL - Gives the base portion of the URL.
Here's a script you can run on an active server page to see the entire Request.Servervariables collection:
For Each name In Request.ServerVariables 
response.write(name&": ")
response.write(Request.ServerVariables(name)&"<h;br>
Next
On a presonal note, here's some of the things I've done using Request.Servervariable info:

  • Filter content by User Agent or IP
  • Ban based on IP
  • Ban bad bots based on User Agent
  • Build URL's based on Script or Server Name
  • Track access by IP

    Here's a cool app built around Request.Servervariables: a dynamic content tree to help your users navigate around your site.
  • No comments: