程序员的出路

alex.w的Blog

导航

asp防止站外提交数据两种方法

Posted on 2008-07-02 23:46  alex.w  阅读(551)  评论(2)    收藏  举报

<%
Function isSelfRefer()
Dim sHttp_Referer, sServer_Name
sHttp_Referer = CStr(Request.ServerVariables("HTTP_REFERER"))
sServer_Name = CStr(Request.ServerVariables("SERVER_NAME"))
If Mid(sHttp_Referer, 8, Len(sServer_Name)) = sServer_Name Then
IsSelfRefer = True
Else
IsSelfRefer = False
End If
End Function


if isSelfRefer() then
response.write "ok!"
else
response.write "去你的!"
end if

%>

把以上代码放到aa.asp,如果是直接输入网址或者是从外部网部链接到本站,http://doamain/aa.asp 就会显示"去你的",

如果系从本站链接到aa.asp,或通过表单提交到aa.asp,将会显示ok

可以防止一些伪造表单向站内提交数据

--------------------------

check_out_post.asp

[code]<!--使用该页进行表单的验证,只需在需验证页包含该页即可.-->
<%
Function check_addr()
Dim server_v1,server_v2
check_addr=False
server_v1=Cstr(Request.ServerVariables("HTTP_REFERER"))
server_v2=Cstr(Request.ServerVariables("SERVER_NAME"))
If Cstr(Mid(server_v1,8,Len(server_v2)))<>Cstr(server_v2) Then
check_addr=False
Else
check_addr=True
End If
End function

Function check_post()
Dim val
val="post" '指定提交方式
check_post=False
If Lcase(Request.ServerVariables("Request_Method"))=val Then
check_post=True
Else
check_post=False
End if
End Function

'以下是调用函数进行检测,如果不满足条件则不执行该网页,否则为通过。
If check_addr()=False Then
response.write "请不要使用外部表单提交数据."
response.End
End If
If check_post()=False Then
response.write "请使用POST方式提交表单数据"
response.End
End If
%>[/code]