Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving and Displaying the ServerVariables collection

ASP 101

Retrieving and Displaying the ServerVariables collection

by  SBendBuckeye  Posted    (Edited  )
Excellent ASP tutorial and code sample site: www.LearnASP.com

Since my main development work does not usually involve ASP, I frequently have to check some reference to refresh my memory on the various ServerVariables maintained by ASP. To avoid that situation in the future, I wrote the following to help jog my memory and saved it in my MiscUtils folder. Hopefully it will be of some benefit to others as well. Enjoy!

The following code spins through the ServerVariables collection and displays selected items.

Code:
<% @LANGUAGE = VBScript %>
<HTML>
<TITLE>Selected Server Variables</TITLE>
<BODY>
<H2>Selected Server Variables</H2>
<%
strText = "QUERY_STRING=<B>" & Request.ServerVariables("QUERY_STRING") & "</B><BR>"
strText = strtext & "APPL_PHYSICAL_PATH=<B>" & Request.ServerVariables("APPL_PHYSICAL_PATH") & "</B><BR>"
strText = strtext & "PATH_INFO=<B>" & Request.ServerVariables("PATH_INFO") & "</B><BR>"
strText = strtext & "PATH_TRANSLATED=<B>" & Request.ServerVariables("PATH_TRANSLATED") & "</B><BR>"
strText = strtext & "LOCAL_ADDR=<B>" & Request.ServerVariables("LOCAL_ADDR") & "</B><BR>"
strText = strtext & "LOGON_USER=<B>" & Request.ServerVariables("LOGON_USER") & "</B><BR>"
strText = strtext & "REMOTE_ADDR=<B>" & Request.ServerVariables("REMOTE_ADDR") & "</B><BR>"
strText = strtext & "REMOTE_HOST=<B>" & Request.ServerVariables("REMOTE_HOST") & "</B><BR>"
strText = strtext & "REMOTE_USER=<B>" & Request.ServerVariables("REMOTE_USER") & "</B><BR>"
strText = strtext & "SCRIPT_NAME=<B>" & Request.ServerVariables("SCRIPT_NAME") & "</B><BR>"
strText = strtext & "SERVER_PROTOCOL=<B>" & Request.ServerVariables("SERVER_PROTOCOL") & "</B><BR>"
strText = strtext & "SERVER_SOFTWARE=<B>" & Request.ServerVariables("SERVER_SOFTWARE") & "</B><BR>"
strText = strtext & "URL=<B>" & Request.ServerVariables("URL") & "</B><BR>"
strText = strtext & "HTTP_REFERER=<B>" & Request.ServerVariables("HTTP_REFERER") & "</B>"
Response.Write strText%>
</BODY>
</HTML>

The following code spins through the ServerVariables collection. Depending on the parameter passed into the code, it either displays all items (the default) or those items which do not have _ALL or ALL_ embedded in the variable name. In the latter case, it also lists those items with no value last in the list (eg "UserName="). Some of this code was adapted from a couple different examples I found at www.LearnASP.com.

Code:
<% @LANGUAGE = VBScript %>
<!-- Sample Call: YourFile.asp?ShowAll=false -->
<html>
<head>
<title>Display Server Variables</title>
</head>
<body bgcolor="#FFFFFF">
<%
Dim MyChoice, MyQuote, MyVar, MyValue, ShowAll, EmptyVars
response.write ("<h2>Server Variables</h2>")
MyChoice = Split(Request.ServerVariables("QUERY_STRING"), "=")
If UBound(MyChoice) < 0 Then 'Default to ShowAll since no argument passed
    ShowAll = True
ElseIf InStr("Y,T,1", UCase(Left(MyChoice(1), 1))) > 0 Then 'Allow Yes, True, 1
    ShowAll = True
End If
If ShowAll Then
    '<!-- Spin through ALL Server variables -->
    For Each MyVar In Request.ServerVariables
       MyValue = Request.ServerVariables(MyVar)
       response.write MyVar & "=<b>" & MyValue & "</b><br>"
    Next
Else
    EmptyVars = "<P><B>Blank Server Variables</b><br>" & vbCrLf
    MyQuote = Chr(34)
    '<!-- Spin through ALL Server variables placing empty ones at end of list -->
    For Each MyVar In Request.ServerVariables
       If InStr(MyVar, "_ALL") + InStr(MyVar, "ALL_") = 0 Then
          MyValue = Trim(Request.ServerVariables(MyVar))
          If Len(MyValue) = 0 Then
             EmptyVars = EmptyVars & MyVar & ", "
          Else
             response.write "request.servervariables(" & MyQuote
             response.write MyVar & MyQuote & ") "
             response.write " =<br><B>" & MyValue & "</b><p>" & vbCrLf
          End If
       End If
    Next
    'Remove trailing ", "
    response.write Left(EmptyVars, Len(EmptyVars) - 2)
End If
%>
</body>
</html>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top