(3 ratings)   
By: msconline.maconstate.edu
As described on the previous page, the ServerVariables Collection within the Request Object is an array of values identified by names. As such, this collection is what is traditionally called an "associative array," meaning a data structure...
Added: 25 June 2008    Views: 340  
PathComputers    Programming    Asp
Keywords: computers   programming   language   asp   database   functions   code   coder  
Do you like this tutorial? Now you can support our team to add more :     
 
 
 

As described on the previous page, the ServerVariables Collection within the Request Object is an array of values identified by names. As such, this collection is what is traditionally called an "associative array," meaning a data structure of values indexed by names. This array of values can be visualized as follows:

Request.ServerVariables Collection
Name
HTTP_REFERER
HTTP_USER_AGENT
REMOTE_ADDR
...
Value
http://msconline.maconstate.edu/Tutorials/ASP/menu.htm
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14
79.112.10.64
...

Any value in the collection is referenced by its associated name:

Request.ServerVariables("name")

Thus, a reference to Request.ServerVariables("HTTP_USER_AGENT") is a reference to the value "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14" which is associated with this name.

In most cases, you will know the name of the value in a collection and will use this name in a script to reference its associated value. In other circumstances--and for other collections in this and in other ASP objects--you might NOT know the names of the variables in the collection, but still need to know what they are and what are their values.

The For Each...Next Loop

There is an important VBScript structure to iterate through all of the items in a collection to discover their names and values. This method will prove very useful in later scripting, but can be illustrated with the Request.ServerVariables Collection.

You iterate through an object's collection by using the special For Each...Next loop. Its general format is

For Each variable in collection
     ...
Next

where variable is a name you supply and collection is the name of the particular collection of values through which you wish to iterate. The variable name that you supply becomes the script reference to the names of items in the collection. Each time through the loop, variable takes on the value of the name of the next item in the collection. Any script reference to variable, then, is a reference to an item name.

Displaying Collection Names

Let's use this iteration structure to increment through each of the items in the Request.ServerVariables Collection and display the names of all its values:

<%
Response.Write("<b>Names in Request.ServerVariables Collection:</b><br>")
For Each Item in Request.ServerVariables
  Response.Write(Item & "<br>")
Next
%>

In this script, the variable named Item is supplied as the variable which will contain the names of the items in the collection as the script iterates through all items. The statement Response.Write(Item & "<br>") displays this item name (displays the contents of variable Item containing the name) along with a line break character so that each name appears on a separate line. This script produces the following display:

Names in Request.ServerVariables Collection:
ALL_HTTP
ALL_RAW
APPL_MD_PATH
APPL_PHYSICAL_PATH
AUTH_PASSWORD
AUTH_TYPE
AUTH_USER
CERT_COOKIE
CERT_FLAGS
CERT_ISSUER
CERT_KEYSIZE
CERT_SECRETKEYSIZE
CERT_SERIALNUMBER
CERT_SERVER_ISSUER
CERT_SERVER_SUBJECT
CERT_SUBJECT
CONTENT_LENGTH
CONTENT_TYPE
GATEWAY_INTERFACE
HTTPS
HTTPS_KEYSIZE
HTTPS_SECRETKEYSIZE
HTTPS_SERVER_ISSUER
HTTPS_SERVER_SUBJECT
INSTANCE_ID
INSTANCE_META_PATH
LOCAL_ADDR
LOGON_USER
PATH_INFO
PATH_TRANSLATED
QUERY_STRING
REMOTE_ADDR
REMOTE_HOST
REMOTE_USER
REQUEST_METHOD
SCRIPT_NAME
SERVER_NAME
SERVER_PORT
SERVER_PORT_SECURE
SERVER_PROTOCOL
SERVER_SOFTWARE
URL
HTTP_CONNECTION
HTTP_KEEP_ALIVE
HTTP_CONTENT_LENGTH
HTTP_ACCEPT
HTTP_ACCEPT_CHARSET
HTTP_ACCEPT_ENCODING
HTTP_ACCEPT_LANGUAGE
HTTP_COOKIE
HTTP_HOST
HTTP_REFERER
HTTP_USER_AGENT

Displaying Collection Values

It is also possible to display the values associated with these variable names. Let's create a script to display both the names and values in the Request.ServerVariables Collection and to format the output in a table:

<table border="1" width="100%">
<caption><b>Values in Request.ServerVariables Collection</b></caption>
<tr>
   <th>Name</th>
   <th>Value</th>
</tr>
<% For Each Item in Request.ServerVariables %>
   <tr>
      <td><%=Item%></td>
      <td><%=Request.ServerVariables(Item)%></td>
   </tr>
<% Next %>
</table>

Values in Request.ServerVariables Collection
Name Value
ALL_HTTP HTTP_CONNECTION:keep-alive HTTP_KEEP_ALIVE:300 HTTP_CONTENT_LENGTH:0 HTTP_ACCEPT:text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 HTTP_ACCEPT_CHARSET:ISO-8859-1,utf-8;q=0.7,*;q=0.7 HTTP_ACCEPT_ENCODING:gzip,deflate HTTP_ACCEPT_LANGUAGE:en-us,en;q=0.5 HTTP_COOKIE:ASPSESSIONIDCARCTRBB=KDDPADABEOJLNNBONMNPLMOD HTTP_HOST:msconline.maconstate.edu HTTP_REFERER:http://msconline.maconstate.edu/Tutorials/ASP/menu.htm HTTP_USER_AGENT:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14
ALL_RAW Connection: keep-alive Keep-Alive: 300 Content-Length: 0 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Accept-Encoding: gzip,deflate Accept-Language: en-us,en;q=0.5 Cookie: ASPSESSIONIDCARCTRBB=KDDPADABEOJLNNBONMNPLMOD Host: msconline.maconstate.edu Referer: http://msconline.maconstate.edu/Tutorials/ASP/menu.htm User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14
APPL_MD_PATH /LM/W3SVC/1/Root/Tutorials
APPL_PHYSICAL_PATH E:\WebSite\Tutorials\
AUTH_PASSWORD
AUTH_TYPE
AUTH_USER
CERT_COOKIE
CERT_FLAGS
CERT_ISSUER
CERT_KEYSIZE
CERT_SECRETKEYSIZE
CERT_SERIALNUMBER
CERT_SERVER_ISSUER
CERT_SERVER_SUBJECT
CERT_SUBJECT
CONTENT_LENGTH 0
CONTENT_TYPE
GATEWAY_INTERFACE CGI/1.1
HTTPS off
HTTPS_KEYSIZE
HTTPS_SECRETKEYSIZE
HTTPS_SERVER_ISSUER
HTTPS_SERVER_SUBJECT
INSTANCE_ID 1
INSTANCE_META_PATH /LM/W3SVC/1
LOCAL_ADDR 168.16.190.175
LOGON_USER
PATH_INFO /Tutorials/ASP/ASP02/asp02-04.asp
PATH_TRANSLATED E:\WebSite\Tutorials\ASP\ASP02\asp02-04.asp
QUERY_STRING
REMOTE_ADDR 79.112.10.64
REMOTE_HOST 79.112.10.64
REMOTE_USER
REQUEST_METHOD GET
SCRIPT_NAME /Tutorials/ASP/ASP02/asp02-04.asp
SERVER_NAME msconline.maconstate.edu
SERVER_PORT 80
SERVER_PORT_SECURE 0
SERVER_PROTOCOL HTTP/1.1
SERVER_SOFTWARE Microsoft-IIS/6.0
URL /Tutorials/ASP/ASP02/asp02-04.asp
HTTP_CONNECTION keep-alive
HTTP_KEEP_ALIVE 300
HTTP_CONTENT_LENGTH 0
HTTP_ACCEPT text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
HTTP_ACCEPT_CHARSET ISO-8859-1,utf-8;q=0.7,*;q=0.7
HTTP_ACCEPT_ENCODING gzip,deflate
HTTP_ACCEPT_LANGUAGE en-us,en;q=0.5
HTTP_COOKIE ASPSESSIONIDCARCTRBB=KDDPADABEOJLNNBONMNPLMOD
HTTP_HOST msconline.maconstate.edu
HTTP_REFERER http://msconline.maconstate.edu/Tutorials/ASP/menu.htm
HTTP_USER_AGENT Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14

Scripting Table Rows. The previous code shows a useful scripting variation in which a script is integrated within HTML code, in this instance to produce a variable number of table rows depending on the number of entries in the collection. The table definition along with its caption and column headings are hard-coded in HTML. The HTML coding for a single table row appears inside the For Each...Next loop so that a row is formatted during each iteration of the loop. In this case the row contains the name and value of an entry in the Request.ServerVariables Collection. The shorthand method of embedding server variables is used to display these names and values inside the row cells. Following completion of the iteration through all server variables, the closing table tag is coded. You will find this type of iteration structure coded within an HTML table to be useful in many applications in which tables are populated by server data.

Remember, a reference to a value in the ServerVariables Collection is in the format Request.ServerVariables("name"). Since these names appear in script variable Item as the script iterates through the collection, a reference to Request.ServerVariables(Item), then, is a reference to the value associated with the item name currently stored in Item. (Note that there are no quotes surrounding Item; this is a variable, not the literal name of an item in the collection.)

Again, these are "live" values resulting from your URL request for this page. We don't have the need or space to discuss all of these variables. You can look over the list to determine those that might be important for your scripts.

ASP Session ID

Notice the HTTP_COOKIE variable near the end of the listing. Its value begins with "ASPSESSIONID...". ASP keeps track of all users who arrive at a Web site by assigning them unique ID values called "Session IDs." This value is written as a Cookie to the user's PC so that any time a user makes a URL request ASP reads the Cookie, knows who made the request, and is able to keep track of individual users and their processing status. The actual value stored in the Cookie is a random number (your current session ID is Session.SessionID=271643451) that is encrypted in this listing. We'll have much more to say about ASP Sessions later in this tutorial.

About the Author :
msconline.maconstate.edu
Iterating through Collections In ASP
Articles and Tutorials Directory by www.learnfobia.com
 Rate this tutorial : Rate 1Rate 2Rate 3Rate 4Rate 5
  |    Add to Favorites
  |    Send to Friend
  |    Print
Comments