When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Wednesday, November 10, 1999

Accessing HTTP Headers and Environment Variables


One of the commonly used collections of the Request object is the ServerVariables collection. For those unfamiliar with this collection, please read Using the Request.ServerVariables Object, which discusses this collection. (Interesting to note, the Using the Request.ServerVariables Object article was the third ever article written for 4Guys!)

- continued -

The Request.ServerVariables collection contains a number of HTTP Headers and environment variables. It is important to understand that these are two entirely different things. The HTTP Headers are additional information the browser sends to the web server when requesting an web page, while the environment variables contain site-wide information stored by the web server. In this article we will examine first the HTTP headers and then the environment variables.

HTTP Headers:
Whenever you visit a web page, a complicated dialog occurs between the client (your browser) and the server (the web server that contains the web page you are requesting). This dialog looks something like this:

  • Client: Connect to Server
  • Server: Accept connection
  • Client: Send HTTP Headers, finishing with a request for the web page
  • Server: Log these HTTP headers, retrieve the web page and send it to the client
  • Client: disconnect from the server and render the HTML received

In fact, you can actively participate in this dialog if you have access to a UNIX machine. Logon, and enter the following command at the prompt:

telnet www.4guysfromrolla.com 80

This will connect to the 4GuysFromRolla.com web server on port 80 (the default HTTP port). Once you connect to the server, the server waits for you to make your request. Begin by entering in the HTTP headers you want to send. There are several common HTTP headers, the two most common ones being User-Agent and Referer. User-Agent contains information about the browser while Referer, if it exists, indicates the URL that contained the link that the user clicked on to reach the given page. To send the User-Agent and Referer headers, we would type in:

User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)
Referer: http://www.go.com

Next, we would need to specify the web page we were interested in retrieving. If we want to obtain the default document, we can do:

GET /

Now, hit enter twice, and you'll see the HTML for http://www.4GuysFromRolla.com/index.shtml! Neat, eh? This is the process that your browser goes through each time you visit a web page! Hopefully this gives you a bit more appreciation for what a web browser needs to do to view a web page! Not only does this dialog need to occur, but a complex parsing algorithm needs to be used to convert the HTML into those pretty animated GIFs and Frames we all love... :)

As I mentioned, the two most common HTTP Headers are Referer and User-Agent. An HTTP header can be obtained from the Request.ServerVariables collection using the following syntax:

Request.ServerVariables("HTTP_HTTP Header Name")

So, if you want to display the Referer HTTP Header on an ASP page, you can do:

<%=Request.ServerVariables("HTTP_Referer")%>

For a good article on a practical use of the Referer HTTP Header, check out Joao Vieira's Two Ways to Protect your ASP Pages. (Note: Cookies are also pass from the client to the server through HTTP headers!)

Environment Variables
Along with the HTTP Headers, the Request.ServerVariables collection contains a number of site-wide environment variables. These environment variables contain information like the web server software being used (Server_Software), what HTTP port is being used (Server_Port), the URL of the currently processing ASP page (Script_Name), the physical path of the root web application (Appl_Physical_Path), and others. To reference an environment variable from the Request.ServerVariables collection, use the following syntax:

Request.ServerVariables("Environment Variable Name")

So, to list the IP Address of the client requesting a web page, you can use:

<%=Request.ServerVariables("REMOTE_ADDR")%>

In fact, you can list all of the environment variables and HTTP headers using the following code:

<TABLE>
      <TR>
           <TD>
                <B>Server Varriable</B>
           </TD>
           <TD>
                <B>Value</B>
           </TD>
      </TR>

      <% For Each name In Request.ServerVariables %>
      <TR>
           <TD>
                <%= name %>
           </TD>
           <TD>
                <%= Request.ServerVariables(name) %>
           </TD>
      </TR>
      <% Next %>
</TABLE>

Happy Programming!


Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES