限制一个ip只能访问一次,现在将asp代码分享给大家:
原理:当用户第一次访问的时候,得到用户的IP,然后把IP存到Iplist.txt中去。下次再访问的时候,先去Iplist.txt中查询他的IP是否存在,正在的话就不允许访问
<%
'
'// //
'//作用:一个IP地址只允许访问本页一次 //
'//引用: //
'// //
'
'Response.Charset = 936 '设置输出编码为简体中文
'Response.Buffer = false '关闭缓冲区
Dim Fso,ts,IpList,Cfs
'设置Cookies函数
Function SetCookie()
Response.Cookies("IsBrow") = "Brow"
Response.Cookies("IsBrow").Expires = Date+365
End Function
'记录IP地址函数
Function WriteIp(FileName, IpAddress)
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
Set ts = Fso.OpenTextFile(Server.MapPath(FileName),8,true)
ts.WriteLine IpAddress
ts.Close
Set ts = Nothing
Set Fso = Nothing
End Function
'读取IP地址函数
Function ReadIpList(FileName)
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
If Not Fso.FileExists(Server.MapPath(FileName)) Then
CreateFile("Iplist.txt")
Exit Function
End If
Set ts = Fso.OpenTextFile(Server.MapPath(FileName))
Iplist = ts.ReadAll
ts.Close
Set ts = Nothing
Set Fso = Nothing
ReadIpList = Iplist
End Function
'创建文件函数
Function CreateFile(FileName)
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
Set Cfs = Fso.CreateTextFile(Server.MapPath(FileName))
Cfs.Close
Set Cfs = Nothing
Set Fso = Nothing
End Function
'关闭当前IE窗口函数(注:IE6下通过,其他浏览器未测试)
Function CloseWindow()
'Response.Write ""
Response.Redirect "http://www.baidu.com"
End Function
Ip = Request.ServerVariables("REMOTE_ADDR") '获取浏览者IP地址
Cookie = Request.Cookies("IsBrow") '获取当前Cookies
'Response.Write Cookie
If Request.ServerVariables("HTTP_X_FORWARDED_FOR") <> "" Then
Response.Write "本站不允许使用代理访问"
Response.End()
Else
If Cookie = "Brow" Then
CloseWindow()
Else
If Instr(ReadIpList("Iplist.txt"),Ip) <> 0 Then
CloseWindow()
Else
WriteIp "Iplist.txt" , Ip
End If
SetCookie()
End If
End If
%>