当前位置: 移动技术网 > IT编程>开发语言>Asp > asp防止刷新功能实现代码

asp防止刷新功能实现代码

2017年12月12日  | 移动技术网IT编程  | 我要评论
使用说明 1.在要保护的页面顶部加如对antirefresh.asp文件的引用如: <!--#include virtual="antirefresh.asp" --
使用说明
1.在要保护的页面顶部加如对antirefresh.asp文件的引用如:
<!--#include virtual="antirefresh.asp" -->
2.接着添加调用代码
复制代码 代码如下:

<%
const varnamedatearr="www_domai_net_app_dataarr" '队列名称
const varnameiparr="www_domai_net_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="www_domai_net_app_dataarr" '队列名称
const varnameiparr="www_domai_net_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="www_domai_net_app_dataarr_search" '(_list)
const varnameiparr="www_domai_net_app_iparr_search" '(_list)
唠叨这些是给那么不太懂的朋友,以便他们能使用这些代码,如果你懂asp那么以上的对你来说就很好理解了。
下面是antirefresh.asp文件源码
复制代码 代码如下:

<%
'***************************************
'* 页面防刷新模块 *
'* wdfrog,2007-8-16
'***************************************
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即可

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网