INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
Are you a Computer / IT professional? Join Tek-Tips now!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Feedback
"...This site is a great forum to exchange knowledge..."
Geography
Where in the world do Tek-Tips members come from?
|
Microsoft: ASP (Active Server Pages) FAQ
|
ASP 101
|
Retrieving and Displaying the ServerVariables collection
Posted: 10 Dec 03
|
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.
<% @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.
<% @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>
|
Back to Microsoft: ASP (Active Server Pages) FAQ Index
Back to Microsoft: ASP (Active Server Pages) Forum |
|
 |
|
Join Tek-Tips® Today!
Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
Talk To Other Members
- Notification Of Responses To Questions
- Favorite Forums One Click Access
- Keyword Search Of All Posts, And More...
Register now while it's still free!
Already a member? Close this window and log in.
Join Us Close