(4 ratings)   
By: msconline.maconstate.edu
The ASP Response Object supplies several useful properties and methods for producing server output destined for a Web page. For example, we have used the Response.Write() method to display server data and to write HTML code to a page. As we proceed...
Added: 25 June 2008    Views: 363  
PathComputers    Programming    Asp
Keywords: computers   programming   language   asp   database   functions   code   coder   variables   server  
Do you like this tutorial? Now you can support our team to add more :     
 
 
 

The ServerVariables Collection

Another useful built-in ASP object is the Request Object. This object deals with server input, with information transmitted from a Web page to the server. This information is packaged within collections, which are arrays of values collected from a user's URL request for a Web page. Among other features, the Request Object contains the ServerVariables collection. This set of items contains information taken from HTTP headers that are transmitted from the browser to the server when the user makes a URL request. The collection contains information about the browser being used along with other information about the page being accessed.

Among the 50 or so items in the ServerVariables collection, the following ones are particularly useful:

ServerVariable Description
HTTP_REFERER The URL of the page containing the link to this page.
HTTP__USER_AGENT The type of browser being used by the visitor.
REMOTE_ADDR The IP address of the visitor.
SERVER_NAME The IP address or Domain Name of the server.
SCRIPT_NAME The virtual (Web) path to and identification of the current page.
PATH_TRANSLATED The physical path to and identification of the current page.

Values in this collection are accessed by using the reference

Request.ServerVariables("variableName")

This is a reference to the Request object, the ServerVariables collection, and the specific name of the variable in the collection. We can display the value of any variable by using this reference within a Response.Write() statement. Thus, if we code

<% Response.Write(Request.ServerVariables("HTTP_USER_AGENT")) %>

we produce the output

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 the value of the server variable HTTP_USER_AGENT identifying the browser being used when a request was made for this page. This reference is, of course, to the browser you are currently using.

Determining Browsers

For certain ASP applications it may be necessary to know which browser the user is running. Because of incompatibilities between Internet Explorer and Navigator and because of differences in their versions, you may need to provide different coding for different browsers and versions. Therefore, you can include an ASP script on a page to determine a user's browser and to provide alternate coding or alternate pages for different browsers.

The HTTP_USER_AGENT server variable is used to discover these browser differences. The values that are returned by this variable are similar to the following:

Internet Explorer: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)
Navigator: Mozilla/4.51 [en] (Win95; I)

It is a matter, then, of parsing these string values to determine which browser is being used.

Among other differences in the values, note that Internet Explorer is identified by the substring "MSIE" which appears for all versions of that browser. Navigator does not produce this substring. So, one way to identify Internet Explorer is to see if "MSIE" appears in the value returned by the HTTP_USER_AGENT variable. Otherwise, this is Netscape Navigator or a compatible browser.

VBScript has an InStr function that checks for the presence of a substring within a text string. The general format for this function is:

InStr(string,substring)

The function checks for the presence of substring within string. If it locates the substring of characters, it returns the starting postion of the substring within the string. If the substring of characters is not present within string, then the value 0 is returned. So, the following script

<%
If InStr(Request.ServerVariables("HTTP_USER_AGENT"),"MSIE") <> 0 Then
   Response.Write("<b>You are using Microsoft Internet Explorer.</b>")
Else
  Response.Write("<b>You are using Netscape Navigator.</b>")
End If
%>

produces the following output for the browser you are currently using:

You are using Netscape Navigator.

Thus, at the top of your ASP pages you can code a routine such as the following:

<%
If InStr(Request.ServerVariables("HTTP_USER_AGENT"),"MSIE") <> 0 Then
     Browser = "IE"
Else
     Browser = "NN"
End If
%>

Then, throughout your page you can apply different processing or formatting by checking the value of variable Browser:

<% If Browser = "IE" Then %>
  ...apply Internet Explorer processing or formatting
<% Else %>
  ...apply Navigator processing or formatting
<% End If %>

Determining The Browser Version

You can also determine the browser version by referencing the HTTP_USER_AGENT substring containing the version number. In modern browsers these values are at postion 31 (for three characters) in Internet Explorer and position 9 (for four characters) in Navigator:

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)
Mozilla/4.51 [en] (Win95; I)

You can pick out these version numbers using the VBScript Mid function to extract a substring from a string. Its general format is

Mid(string, start, length)

where string is the string containing the substring of characters, start is the starting position of the substring, and length is the number of characters to extract as the substring. The following script first checks to see whether the visitor's browser is Internet Explorer or Navigator and then extracts the version number substring, assigning it to variable Version:

<%
If InStr(Request.ServerVariables("HTTP_USER_AGENT"),"MSIE") <> 0 Then
  Browser = "Internet Explorer"
  Version = Mid(Request.ServerVariables("HTTP_USER_AGENT"),31,3)
Else
  Browser = "Navigator"
  Version = Mid(Request.ServerVariables("HTTP_USER_AGENT"),9,4)
End If
Response.Write("<b>Your browser is " & Browser & " " & Version & ".</b>")
%>

Your browser is Navigator 5.0 .

Determining Other Header Information

There is additional useful information in the URL headers transmitted to the server. The following table shows this information pertinent to your current visit to this page. The values displayed in bold are the real-time server values displayed with Response.Write() statements.

ServerVariable Current Value
HTTP_REFERER http://msconline.maconstate.edu/Tutorials/ASP/menu.htm
(The URL of the page containing the link to this page.)
REMOTE_ADDR 79.112.10.64
(The IP address of the visitor.)
SERVER_NAME msconline.maconstate.edu
(The IP address or Domain Name of the server.)
SCRIPT_NAME /Tutorials/ASP/ASP02/asp02-03.asp
(The virtual (Web) path to and identification of the current page.)
PATH_TRANSLATED E:\WebSite\Tutorials\ASP\ASP02\asp02-03.asp
(The physical path to and identification of the current page.)
About the Author :
msconline.maconstate.edu
Server Variables 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