ASP防止刷新的功能实现方法


ASP #防止刷新2012-05-11 23:39
使用说明 
1.在要保护的页面顶部加如对AntiRefresh.asp文件的引用如: 
<!--#include virtual="AntiRefresh.asp" --> 
2.接着添加调用代码 
<% 
Const VarNameDateArr="yige_org_App_DataArr" '队列名称 
Const VarNameIPArr="yige_org_App_IPArr" '队列名称 
Dim objAntiRefresh 
Set objAntiRefresh= new AntiRefresh 
objAntiRefresh.BufferSize=100 '队列大小 
objAntiRefresh.CacheItemAvailTime=2 '间隔时间 
If Not objAntiRefresh.IsValidAccess() Then 
Set objAntiRefresh=Nothing 
Response.Write("您的访问过去频繁请2秒后再试.") 
Response.End() 
End If 
Set objAntiRefresh=Nothing 
%> 

其中要注意的是 
Const VarNameDateArr="yige_org_App_DataArr" '队列名称 
Const VarNameIPArr="yige_org_App_IPArr" '队列名称 
比方你要在List.asp与search.asp中加入通一个防刷新器,那么你把上面的调用代码分别复制到这两个页面,或者保证两个页面的掉用代码一致。这样的效果是,你访问了List.asp页那么你在2秒内将不能访问List.asp或Search.asp页 
如果你要2个页面独立,即你访问List.asp后,你会在2秒内不能再次访问List.asp,但是你可以访问Search.asp,反之毅然,那么你就要保证2个页面的 Const VarNameDateArr="xxx",Const VarNameIPArr="xxx"不同,比方第一个也面你用xxxList,第个个页面用xxxSearch, 
如: 
Const VarNameDateArr="yige_org_App_DataArr_Search" '(_List) 
Const VarNameIPArr="yige_org_App_IPArr_Search" '(_List) 
一个网yige.org唠叨这些是给那么不太懂的朋友,以便他们能使用这些代码,如果你懂Asp那么以上的对你来说就很好理解了。 


下面是AntiRefresh.asp文件源码 
<% 
'*************************************** 
'* 页面防刷新模块 * 
'*************************************** 
Class AntiRefresh 
Private IPArr,DateArr 
Private m_BufferSize 
Private m_CacheItemAvailTime 
Private Sub Class_Initialize() 
Application.Lock() 
m_BufferSize=100 
m_CacheItemAvailTime=2 
End Sub 
Private Sub Class_Terminate() 
Application.UnLock() 
End Sub 
Public Property Get CacheItemAvailTime 
CacheItemAvailTime=m_CacheItemAvailTime 
End Property 
Public Property Let CacheItemAvailTime(Value) 
m_CacheItemAvailTime=Value 
End Property 
Public Property Get BufferSize 
BufferSize=m_BufferSize 
End Property 
Public Property Let BufferSize(Value) 
m_BufferSize=Value 
End Property 
Private Sub EnsureArr() 
If IsArray(Application(VarNameDateArr)) Then 
DateArr=Application(VarNameDateArr) 
Else 
ReDim DateArr(BufferSize) 
End If 
If IsArray(Application(VarNameIPArr)) Then 
IPArr=Application(VarNameIPArr) 
Else 
ReDim IPArr(BufferSize) 
End If 
End Sub 
Public Function IsValidAccess() 
Dim ip,i 
ip=GetIP() 
IsValidAccess=True 
EnsureArr() 
For i=1 To BufferSize 
If IPArr(i)=ip Then 
If DateDiff("s",CDate(DateArr(i)),Now()) < CacheItemAvailTime Then 
IsValidAccess=False 
Exit Function 
End If 
End If 
Next 
Call QueueHandle() 
DateArr(1)=Now() 
IPArr(1)=ip 
Application(VarNameIPArr)=IPArr 
Application(VarNameDateArr)=DateArr 
End Function 
Public Function ClearCache() 
Set Application(VarNameDateArr)=Nothing 
Set Application(VarNameIPArr)=Nothing 
End Function 
Private Sub QueueHandle() 
Dim i,j 
For i=BufferSize-1 To 1 Step -1 
DateArr(i+1)=DateArr(i) 
Next 
For j=BufferSize-1 To 1 Step -1 
IPArr(j+1)=IPArr(j) 
Next 
End Sub 
Private Function GetIP() 
Dim strIPAddr 
If Request.ServerVariables("HTTP_X_FORWARDED_FOR") = "" OR InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), "unknown") > 0 Then 
strIPAddr = Request.ServerVariables("REMOTE_ADDR") 
ElseIf InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",") > 0 Then 
strIPAddr = Mid(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), 1, InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",")-1) 
ElseIf InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ";") > 0 Then 
strIPAddr = Mid(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), 1, InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ";")-1) 
Else 
strIPAddr = Request.ServerVariables("HTTP_X_FORWARDED_FOR") 
End If 
GetIP = (Trim(Mid(strIPAddr, 1, 30))) 
End Function 
End Class 
%> 


asp防止刷新
Sub chkreflash() 
dim ScriptName 
ScriptName=lcase(request.ServerVariables("PATH_INFO")) 
dim posttime,DoReflashPage,ReflashPage,SplitReflashPage 
posttime=1 '防止刷新时间 
DoReflashPage=false 
ReflashPage="|Article_one.asp" 
SplitReflashPage=split(ReflashPage,"|") 
for i=0 to ubound(SplitReflashPage) 
if instr(scriptname,SplitReflashPage(i))>0 then 
DoReflashPage=true 
exit for 
end if 
next 
if (not isnull(session("ReflashTime"))) and posttime>0 and DoReflashPage then 
if DateDiff("s",session("ReflashTime"),Now())<posttime then 
call msg("警告!系统已记录您的IP,和刷新次数。\n\n请不要在"&posttime;&"秒内连续刷新本页面!","-1") 
else 
session("ReflashTime")=Now() 
end if 
elseif isnull(session("ReflashTime")) and posttime>0 and DoReflashPage then 
Session("ReflashTime")=Now() 
end if 
End Sub

调用方法在页面首部加入chkreflash即可

相关文章

粤ICP备11097351号-1