Have a question? ask www.kenlet.com
Home  |  FAQ  |  About  |  Contact  |  View Source   
 
SEARCH:
 
BROWSE:
    My Hood
Edit My Info
View Events
Read Tutorials
Training Modules
View Presentations
Download Tools
Scan News
Get Jobs
Message Forums
School Forums
Member Directory
   
CONTRIBUTE:
    Sign me up!
Post an Event
Submit Tutorials
Upload Tools
Link News
Post Jobs
   
   
Home >  Tutorials >  General Web Development >  How to log IP's (and some other http headers) with ASP/VB
Add to MyHood
How to log IP's (and some other http headers) with ASP/VB   [ printer friendly ]
Stats
  Rating: 2.22 out of 5 by 9 users
  Submitted: 11/09/02
John Woo (woojm@uci.edu)

 
Audience:
Those who know VB and ASP (or ASP programmers in general)
who want to get access to HTTP server variables.

Introduction:
Ever wonder who's visiting your website? Many have resorted to using
services such as IMCHAOS to log people's screen names who have clicked on their other's links. However, many are starting to realize that IMCHAOS is tracking people's names and therefore defeating the whole purpose.

In this tutorial I will show a simple way to get access to
HTTP headers that the user will send to the server. This
will include the IP address along with other information
the user's browser will send to the server.

Note: when taking someone's ip through a webserver, it's always nice to notify the user that you're doing this ;) You may want to put a disclaimer that you will be doing this. Then have them enter your site and log the information at that time.

Here's some of the server variables we'll be working with.
All of these variables are built in to ASP:
Request.ServerVariables("REMOTE_ADDR") -- the IP address of the visitor
Request.ServerVariables("HTTP_USER_AGENT") -- the browser the user uses to visit your site
Request.ServerVariables("HTTP_REFERER") -- where the user was before he/she came to your site



Example:
This example will show how to access these HTTP variables
and then append them to a text file located on the server.
<%
    dim filesys, filetxt
    Const ForWriting = 2, ForAppending = 8
    Set filesys = CreateObject("Scripting.FileSystemObject")
    Set filetxt = filesys.OpenTextFile(Server.MapPath("*PATH*"), ForAppending, True)
    filetxt.WriteLine("<p>")
    filetxt.WriteLine("IP=" & Request.ServerVariables("REMOTE_ADDR"))
    filetxt.WriteLine("<br>")
    filetxt.WriteLine("Date: " & Date() & " at " & Time() &".")
    filetxt.WriteLine("<br>")
    filetxt.WriteLine("HTTP_USER_AGENT=" & Request.ServerVariables("HTTP_USER_AGENT"))
    filetxt.WriteLine("<br>")
    filetxt.WriteLine("HTTP_REFERER=" & Request.ServerVariables("HTTP_REFERER"))
    filetxt.WriteLine("</p>")
    filetxt.Close
    Set filetxt = Nothing
    Set filesys = Nothing

%>

Constraints:
*PATH* - this refers to a path where you can write to.


Explanation:
Everything should be pretty self explanatory, if you know VB and ASP.
The only note I have should be that you can change ForAppending to ForWriting
if your rather write to the file instead of appending.

Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile(Server.MapPath("*PATH*"), ForAppending, True)


Return to Browsing Tutorials

Email this Tutorial to a Friend

Rate this Content:  
low quality  1 2 3 4 5  high quality

Reader's Comments Post a Comment
 
Good tutorial, nice place to get started when using ASP
-- Dan Cramer, November 11, 2002
 
If you're going to rate me down, at least tell me what you don't like about it so I can improve.
-- John Woo, December 05, 2002
 
I got a recent email about my post:

I guess an individual was unclear on how to incorporate this code onto his website. Note that I also assumed that you knew VB and have done ASP programming before.

You need to copy and paste the code (onto any asp/aspx page) listed on devhood (everything in blue) in order for the server to log the HTTP headers to a text file. This text file is located on the server denoted by "*PATH*" (see line 5 to modify this). (instead of *PATH*, put log.txt or something like that

And that's it. you can view the edited file through your web browser or get the code by ftp-ing into your server and viewing the file.
-- John Woo, December 17, 2002
 
Not sure why but it does not work for me. Using IE 6.0 or Mozilla 1.0. I do know VB and ASP. One question is permissions? does IUSER need write or does Everyone write have the proper permisions? In other words is IUSER included in the everyone group? I am using frames on my page and I set the asp Oops never mind my index is an HTML page! DOH!!!
-- Troy M, January 12, 2005
 

Copyright © 2001 DevHood® All Rights Reserved