When discussing the contents of the Request object, one of the collections to study is the ServerVariables collection. This set contains a combination of the value in the HTTP header sent from the client to the server with the page request, and the value provided by the server itself when it receives the request.
The value returned by
the "self-referential" page
in the ServerVariables collection contains the details of the web server and the path information of the current page.This information can be used anywhere you create a page. For example, to create a "self-reference" page that can call itself again 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 HTTP “SCRIPT_NAME” value:
<FORM ACTION="<% = Request.ServerVariables(“SCRIPT_NAME”) %>” METHOD="POST”>
Use 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 examples work even if the name or location of the original page changes, because the current page's path information is used (of course, the second example fails when the name of the detached target page changes).
In other words, if the URL is automatically built for the search engine's sub-session, some values of ServerVariable can be collected:
strFullURL = http:// & Request.ServerVariables("LOCAL_ADDR") _
& ":" & Request.ServerVariables("SERVER_PORT") _
& Request.ServerVariables("PATH_INFO")
This will create a complete URL including the port number (in this case, not the standard value of 80). For example, the result might be:
http://194.74.60.254:1768/thispath/thispage.asp
Detecting the browser version
Another useful value in the ServerVariables collection is the user agent string of the user's browser. On the "Detecting the Browser Type" page (browsertype.asp), the "HTTP_USER_AGENT" value in the ServerVariables collection is used to obtain the user agent string. Some scripts are used to parse this information and find 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 to “_
& “<A HREF=” & Chr(34) & http://www.microsoft.com/ie/ ”_
& Chr(34) & “> http://www.microsoft.com/ie/ <A>
"
intVersion = Cint(Mid(strUA, InStr(strUA, “MSIE”) + 5, 1))
If intVersion >=4 Then
Response.Write “You 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(34) & http://home.netscape.com/ ”_
& “download/”& Chr(34) & “> http://home.netscape.com ”_
& “/download/</A>
"
intVersion = Cint(Mid(strUA, InStr(strUA, “/”) +1, 1))
If intVersion >= 4 Then
Response.Write “You 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(34) & “> http://www.altavista.com/ </A>
"
End If
End If
End If
%>
The search results for IE 5.0 and Navigator 4.61 are different respectively. For browsers from other manufacturers, you can get a link to automatically start searching for the manufacturer's name on the Alta Vista Web site.
Note that Netscape does not provide the manufacturer's name in the user agent string, so there is no absolute guarantee that a browser is Navigator.
Detecting the browser's language
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 is hard-coded into the user's regional version. Examples of language codes are en-us (UK, US), de-at (Germany, Australia) and es-pe (Spain, Peru).
Language codes can be generic and omit dialect identifiers: for example, on our site Wrox, a large number of visitors use en (English) as the language code.
Therefore, the language code can be detected and an appropriate region-specific or language-specific version of the page automatically loaded.
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 redirect the page based on a specific dialect:
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
Other useful ServerVariables collection values
can access and use any member of the ServerVariables collection to control the way the ASP page responds to a request. You can check whether a visitor accessed the site using the default port 80 or another one. In this example, look for access via port 443 - which provides Secure Socket Layer (SSI) access (and other protocols) - and redirect them to an appropriate 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 the browser is required to register and be verified by the server (rather than allowing them to access anonymously under the IUSER account of the web server, this issue will be discussed in detail in a later chapter), the user name can be queried to determine the user who is dealing with us Who is it and whether to load the page to this user. For example, the following code will only display the administration link to users 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 ASP does not fill out the ServerVariables collection until you access one of its members. Accessing a member of this collection for the first time will cause IIS to get all of it, the ServerVariables collection should only be used when needed.
Other Request and Response Techniques
Now, let's look at a few useful techniques for using the Request and Response objects, including:
· Management of connections, buffering, and page redirections.
· Operation of HTTP headers, caching and "expiration" pages.
· Utilize client certificates.
· Create customized log file messages.
1. Management of connections, buffering and page redirections
A very useful feature of ASP is to enable users to redirect from one ASP web page to another web page (ASP or HTML), or to another source file (such as a ZIP file or text file) ). This is transparent to the user, it's actually the browser that does the work. When using the Response.Redirect method to load a new web page, a special HTTP header is actually sent back to the client. This header is:
HTTP/1.1 302 Object Moved
Location /newpath/newpage.asp
The browser reads this header information and loads the page as directed by the Location value. This is functionally the same as using the client-side HTML <META> tag in a Web page, for example:
<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=/newpath/newpage.asp">
This brings a The problem is that the proxy server between the server and the user may provide its own message containing a link to the new page, rather than loading the new page directly. And browsers may do the same job depending on the manufacturer and version. This removes the supposed transparency and makes accessing your site more cumbersome for the user as they continue to receive error messages.
After sending any page content such as text or HTML, we can no longer use the Redirect method. However, one way that appears to limit "proxy server impact" is to first ensure that no output (including HTTP headers) is sent to the client. In ASP 2.0, you must turn on buffering and then use the Clear method to clear the buffer:
Response.Buffer = True
'Some condition to select the appropriate page:
If Request.ServerVariables("SERVER_PORT") = 1856 Then
StrNewPage = “/newpath/this_page.asp”
Else
StrNewPage = “/newpath/the_other_page.asp”
End If
Response.Clear
Response.Redirect strNewPage
In ASP 3.0, buffering is turned on by default, so the first line can be ignored, but it is harmless and ensures that our web page will still work even in an ASP 2.0 environment.
Rather than using this type of HTTP header redirection, it is better to use a new feature of ASP 3.0, which allows us to convert to execute another web page through the Transfer method of the Server object. We will study this issue further in the future.
1) ASP page buffer
As you have seen, ASP 3.0 page buffer is turned on by default in IIS 5.0, and turned off by default in earlier versions. Microsoft tells us that buffering provides more efficient web page delivery in IIS 5.0, which is why the default state of buffering has been changed. In most cases, this has no effect on us. However, if you have a very large web page, or a web page that takes a while to create using ASP or other server-side code and components, as its parts are completed, we can refresh them in batches to the client:
...
... Code to create first part of the page
...
Response.Flush
...
... Code to create next part of page
...
Response.Flush
...
sometimes you may wish to stop code execution at some point before the end of the page, by calling the End method to refresh all current content to the client and abort any further processing.
...
... Code to create first part of the page
If strUserName = "" Then Response.Clear
...
... Code to create a new version of this part of the page
...
Here are two example web pages demonstrating buffering and redirection, which can be downloaded from the main "Response Object" page (sow_response.asp). The first Response.Redirect example web page is named redirect.asp. It inserts some content into the buffered page, clears the buffer, and redirects to another web page:
For intLoop = 1 To 1000000
Response.Write "."
Next
Response.Clear
Response.Redirect "show_redirect.asp"
Response.End
target page show_response.asp does the same job, but the redirection is back to the "Response Object" home page. Because these pages are buffered and all output must be cleared before redirection, there is no visible output in the browser. However, each redirect that occurs can be seen by observing the browser's status. As shown in the figure below:
<img src=/u/info_img/2009-06/25/asp14.jpg>
In the "Response Object" home page, click the "Response.Flush" link to open the second sample web page usebuffer.asp. It simply iterates through each character of a string and flushes them to the client with a certain delay. Although this is a very inefficient use of Web servers and ASP, it demonstrates how buffering works.
<img src=/u/info_img/2009-06/25/asp15.jpg>
The following is the required minimum ASP code. Note that we refresh each character to the browser separately, because otherwise it will be stored In the buffer until the web page is complete:
strText = “This text has been flushed to the browser using “ & _
"<B>Response.Flush</B>
"
For intChar =1 To Len(strText)
For intWrite = 1 To 100000
Next
Response.Write Mid(strText,intChar,1)
Response.Flush
Next
2) Response.IsClientConnected property
The IsClientConnected property already exists in ASP 2.0, but it is somewhat unreliable. Some output must be sent to the client before it returns an accurate result. This problem has been solved in ASP 3.0. This property can now be used freely.
IsClientConnected is a useful way to observe whether the user is still connected to the server and loading the web page created by ASP. If the user disconnects or stops downloading, we no longer have to waste server resources creating the web page because the buffer contents will be discarded by IIS. Therefore, for web pages that require a lot of time to calculate or use a lot of resources, it is worth checking at every stage whether the browser is offline:
...
... Code to create first part of the page
...
If Response.IsClientConnected Then
Response.Flush
Else
Response.End
End If
...
... Code to create next part of page...