ServerVariables集合中,另外一个有用的值是用户浏览器的用户代理字符串。在“Detecting the Browser Type”页面(browsertype.asp),使用ServerVariables集合中的“HTTP_USER_AGENT”值来获得用户代理字符串,一些脚本用来解析该信息并寻找生产厂家名称和浏览器版本。
<% 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 %> 对IE 5.0和Navigator 4.61的搜索结果分别不同,对于其他厂家的浏览器,可以得到一个链接在Alta Vista Web站点自动开始搜索厂家的名称。
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 或者根据特定的方言,重定向页面:
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 其他有用的ServerVariables集合的值
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 假如要求浏览者注册且由服务器验证(而不是允许他们在Web服务器的IUSER帐号下匿名访问,这个问题将在后面章节中详细讨论),可以查询用户名称,来判定正在与我们打交道的用户是谁,是否装载页面给该用户。例如,下面的这个代码将只向名为Administrator的用户显示管理链接。
<% 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 %> ... 注意ASP不填写ServerVariables集合直到你访问其中的一个成员。首次访问该集合的一个成员将使IIS得到它的全部,应只在需要时才使用ServerVariables集合。