搜索文章:

首页  |  Java技术  |  Asp.net  |  Asp编程  |  VC/C++  |  Delphi  |  VB编程

如何用ASP创建日志文件

你是否有时想知道什么人访问你的站点,什么时间,ip地址等。下面我就这个问题向大家来阐述一下。这个例子使用文本文件来写入用户的信息创建一个logfile.asp放在每一个asp的页面的顶端<!--#include file="logfile.asp"-->当有人来访问你的站点logfile.asp自动把他的信息写入logfile.txt,如果相关的url一样的话则不写入文件
file: logfile.asp

<%
dim validentry log variable
first set that this log is valid
validentry = true

if session variable "login" is not empty
that mean this person has already been logged
then set validlog to false
if not isempty(session("login")) then validentry = false

here you can add different restriction
if the refering url is from same site
dont write to log file
if left(request.servervariables("http_referer"), 17)="http://devasp.com" then
validentry = false
end if
if left(request.servervariables("http_referer"), 21)="http://www.devasp.com" then
validentry = false
end if

now if validentry is true then enter to log file
if validentry then
const forappending = 8
const create = true
dim fso
dim ts
dim myfilename
dim strlog

myfilename = server.mappath("mylogfile.txt")
set fso = server.createobject("scripting.filesystemobject")
set ts = fso.opentextfile(myfilename, forappending, create)

store all required values in strlog
strlog = "<br><p><b>" & now & "</b> "
strlog = strlog & request.servervariables("remote_addr") & " "
strlog = strlog & request.servervariables("http_referer") & " "
strlog = strlog & request.servervariables("http_user_agent") & "<br>"
write current information to log text file.
ts.write strlog
ts.writeline ""
create a session varialbe to check next time for validentry
session("login") = "yes"
set ts = nothing
set fso = nothing
end if
%>

()

相关文章:
© 2006   www.java-asp.net