ASP  «Prev  Next»

Lesson 5Server Variables
ObjectiveUse the Request object to retrieve server variables.

Server variables collection in ASP

One of the most useful collections of the Request object previously introduced is the Server Variables collection. It contains all of the server environmental variables as well as the client's Request. The Request information is transmitted to the server via the client's HTTP headers, which contain information about the contents of the request.
Every time an ASP page is executed, the web server creates a set of server variables to accompany that page which can then be examined and manipulated using ASP.
Server variables collection: A series of values representing the properties of a Web server (e.g., operarting system, software version, file system etc.)

Retrieving server variables

The ServerVariables collection contains a name/value associative array[1] of all server variables. You have the option of retrieving a single variable or all variables. If you want to retrieve single variables try the following:

set svrVars = Request.ServerVariables
Response.Write svrVars("HTTP_USER_AGENT") 
Response.Write svrVars("HTTP_UA_OS") 
Response.Write Cstr(svrVars("REMOTE_HOST"))

The above example creates a reference to the ServerVariables collection using the variable svrVars. This is merely used as a shortcut to the ServerVariables collection. An equivalent syntax to retrieve the HTTP_USER_AGENT key would be Request.ServerVariables("HTTP_USER_AGENT"). The Cstr function is used to convert the Remote Host IP address to a string to ensure flexible use of the retrieved value.
If you want to retrieve the entire collection of server variables, try the following:

For each key in Request.ServerVariables
Response.Write key & "=" Request.ServerVariables(key)
Next

Looping through the collection

Since ServerVariables is a collection, we can use the For...Next statement to iterate through each key in the collection.
[1]Associative array: A two-dimensional array that stores the name of a value (such as "FileName") and its associated value (inventory.mdb ).