Listing Server Variables in ASP

September 24, 2005 - 6:24pm — Brandon

The set of server variables contains information like paths, cookie settings, CGI version, and much more. Here I'll explain how to list your server variables.

If you're looking for the value of a certain server variable, you can retrieve it using:
Request.ServerVariables(variable)

For example, if you wanted to find the referring page, you would use:
Request.ServerVariables("HTTP_REFERER")

That might be useful in defining the link for a "back" button, like this:
<% referURL=Request.ServerVariables("HTTP_REFERER") %><a href="<%=referURL %>">Previous Page</a>

Listing All Server Variables

To list all the server variables, you could look up each one and retrieve its value using Request.ServerVariables(variable). However, that's very tedious. Instead, we can write a short script that will loop through all the server variables and print them out. The following does just that.

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  2. "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  3. <html>
  4. <head>
  5. <title>Server Variables</title>
  6. </head>
  7. <body>
  8. <%
  9. For Each Key in Request.ServerVariables
  10. response.write Key & ": "_
  11. & request.servervariables(Key) &_
  12. & "<br /><br />"
  13. Next
  14. %>
  15. </body>
  16. </html>

The output is very plain and straightforward. In most cases this is OK since it's only yourself that would be viewing it. Of course, if you wanted to, you could format it within a table, use CSS or any other way you see fit. I've found it useful to name this file "servervariables.asp" and use it as a handy reference whenever you need path information, the value of all stored cookies, etc.

Share/Save