Using the ServerVariables Collection

Source: Internet
Author: User
Tags chr servervariables port number
server| Collection


When discussing the content of the request object, one of the collections to be studied is the ServerVariables collection. This collection contains a combination of two values, one that accompanies the page request from the client to the server's HTTP header, and the value provided by the server itself when it receives the request. To display the use of values in the ServerVariables collection, in the Request Object page (show_request.asp), click the "ServerVariables examples" link to open another page, as shown in the following illustration:

The window shown in the following illustration shows a subset of some of the most useful values in the ServerVariables collection.


The Self-References page
The value returned in the ServerVariables collection contains the details of the Web server and the path information for the current page. You can use this information to create a page anywhere. For example, to create a "self reference" page, this page can call itself to complete another task, we can use the following code:
<form action= "<% = Request.ServerVariables (" path_info ")%>" method= "POST >
The same effect can be obtained with the "script_name" value of http:
<form action= "<% = Request.ServerVariables (" Script_name ")%>" method= "POST >
Using the <A> element to open a different page, you can use:
...
<%
strFullPath = Request.ServerVariables ("Path_info")
' Strip off the ' file name
Strpathonly = Left (strFullPath, InStrRev (strFullPath, "/"))
Strnextpage = strpathonly & "Pages/next_page.asp"
%>
...
<a href= "<% = Strnextpage%>" >next page</a>
...
These instances work correctly even if the name or location of the original page changes, because the path information for the current page is used (of course, the second example fails when the name of the detached target page changes).
In other words, if you automatically create URLs for a child session of a search engine, you can collect some of the servervariable values:
Strfullurl = http://& Request.ServerVariables ("Local_addr") _
& ":" & Request.ServerVariables ("Server_port") _
& Request.ServerVariables ("Path_info")
This will create a complete URL that includes the port number (in this case, not the standard value 80). For example, the result might be:
Http://194.74.60.254:1768/thispath/thispage.asp

Detecting the version of a browser
Another useful value in the ServerVariables collection is the user agent string for the user's browser. On the detecting the Browser Type page (browsertype.asp), use the "http_user_agent" value in the ServerVariables collection to get the user agent string. Some scripts are used to parse the information and look for the manufacturer name and browser version.
<%
Strua = Request.ServerVariables ("Http_user_agent")
Response.Write "The User Agent string is <B>" & Strua & "</B>

If InStr (Strua, "MSIE") Then
Response.Write "To upgrade your browser go" _
& "<a href=" & Chr (+) & http://www.microsoft.com/ie/"_
& Chr & ">http://www.microsoft.com/ie/<A>

Intversion = Cint (Mid (Strua, InStr (Strua, "MSIE") + 5, 1)
If intversion >=4 Then
Response.Write "can use Microsoft Dynamic HTML"
End If
Else
If InStr (Strua, "Mozilla") Then
If InStr (Strua, "compatible;") = 0 Then
Response.Write "Your Browser is probably Navigator. You can "_
& "Download the latest version of Navigator from" _
& "<a href=" & Chr (+) & http://home.netscape.com/"_
& "download/" & Chr (+) & ">http://home.netscape.com" _
& "/download/</a>

Intversion = Cint (Mid (Strua, InStr (Strua, "/") +1, 1))
If intversion >= 4 Then
Response.Write "Can probably use Netscape Dynamic HTML"
End If
Else
Strversion = Mid (Strua, InStr (Strua, "compatible;") + 12)
Strproduct = Left (strversion, InStr (Strversion, ""))
Response.Write "Your Browser is navigator-compatible. You can "_
& "Search for the" manufacturer using a search engine, such as "_
& "<a href=" & Chr (34) _
& "Http://www.altavista.digital.com/cgi-bin/query?q=" _
& Strproduct _
& Chr & ">http://www.altavista.com/</A>

End If
End If
End If
%>
For IE 5.0 and navigator 4.61 search results are different, for other manufacturers of browsers, you can get a link in the Alta Vista Web site automatically start searching for the name of the manufacturer.


Note that Netscape does not provide a manufacturer's name in the user agent string, so it is not absolutely guaranteed that a browser must be navigator.

Detecting the language of a browser
Another useful value in the ServerVariables collection is "Http_accept_language", which contains a language code that is specified when the browser is installed, or hard-coded into the user's regional version. Examples of language codes are en-US (UK, USA), De-at (Germany, Australia) and ES-PE (Spain, Peru).
Language codes can be generic and omit dialect identities: for example, in our site Wrox, a large number of visitors are en (English) as the language code.
Therefore, you can detect the language code and automatically load a suitable locale or a specific language version of the page.
Strlocale = Lcase (Left (Request.ServerVariables ("Http_accept_language"), 2)
Select Case Strlocale
Case "en": Response.Redirect "http://uk_site.co.uk/"
Case "de": Response.Redirect "http://de_site.co.de/"
Case "FR": Response.Redirect "http://fr_site.co.fr/"
' ... etc
Case Else:Response.Redirect "http://us_sitel.com/"
End Select
Or, depending on the specific dialect, redirect the page:
Strlocale = Lcase (Request.ServerVariables ("Http_accept_language"))
Select Case Strlocale
Case "EN-GB": Response.Redirect "http://uk_site.co.uk/"
Case "en-us": Response.Redirect "http://us_site.com/"
Case "Es-pe": Response.Redirect "http://es_site2.co.pe/"
‘...
Case Else:Response.Redirect "http://us_site1.com/"
End Select

Values for other useful servervariables collections
You can access and use any member of the ServerVariables collection to control how the ASP page responds to a request. You can check whether a browser is using the default port 80 or another when visiting the site. In this example, look for access through port 443-this port provides Secure Sockets Layer (Secure Socket Layer,ssi) access (and other protocols) and redirects them to a corresponding page.
If Request.ServerVariables ("server_port") = "443") Then
Response.Redirect "/securesite/default.asp" ' Secure user
Else
Response.Redirect "/normalsite/default.asp" ' Non-secure user
End If
If browsers are required to register and authenticate by the server (rather than allowing them to access anonymously under the Iuser account of the Web server), this issue will be discussed in more detail in later chapters, and the user name can be queried to determine who the user is dealing with and whether to load the page to the user. For example, the following code will only show the admin link to the user named Administrator.
...
<a href= "dispcnfg.asp" >change Display configuration</a>

<a href= "dispcolr.asp" >change Display colors</a>

<a href= "keyboard.asp" >change keyboard configuration</a>

<%
If Request.ServerVariables ("Auth_User") _
= Ucase (Request.ServerVariables ("SERVER_NAME")) & "Administrator" Then
%>
<a href= "allusers.asp" >administer all users</a>

<a href= "usrlogon.asp" >administer Logon information</a>
<%
End If
%>
...
Note that the ASP does not fill in the ServerVariables collection until you access one of the members. Accessing one member of the collection for the first time will give IIS all of it and use the ServerVariables collection only when needed.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.