您的位置:首页 > 编程语言 > ASP

ASP防止盗链或防止下载的方法

2007-02-12 15:07 549 查看
我们在管理网站文件时,可以把扩展名一样的文件放在同一个目录下,起一个比较特别名字,例如放pdf文件目录为the_pdf_file_s,把下面代码另存为down.asp,他的网上路径为http://www.xx.com/down.asp,我们就可以用http://www.xx.com/down.asp?FileName=51windows.pdf来下载这个文件了,而且下载者无法看到这个文件实际下载路径的!在down.asp中我们还可以设置下载文件是否需要登陆,判断下载的来源页是否为外部网站,从而可以做到防止文件被盗链。

示例代码:
以下是引用片段:

以下是引用片段:
<%
From_url = Cstr(Request.ServerVariables("HTTP_REFERER"))
Serv_url = Cstr(Request.ServerVariables("SERVER_NAME"))
if mid(From_url,8,len(Serv_url)) <> Serv_url then
response.write "非法链接!" '防止盗链
response.end
end if

if Request.Cookies("Logined")="" then
response.redirect "/login.asp" '需要登陆!
end if
Function GetFileName(longname)'/folder1/folder2/file.asp=>file.asp
while instr(longname,"/")
longname = right(longname,len(longname)-1)
wend
GetFileName = longname
End Function
Dim Stream
Dim Contents
Dim FileName
Dim TrueFileName
Dim FileExt
Const adTypeBinary = 1
FileName = Request.QueryString("FileName")
if FileName = "" Then
Response.Write "无效文件名!"
Response.End
End if
FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
Select Case UCase(FileExt)
Case "ASP", "ASA", "ASPX", "ASAX", "MDB"
Response.Write "非法操作!"
Response.End
End Select
Response.Clear
if lcase(right(FileName,3))="gif" or lcase(right(FileName,3))="jpg" or lcase(right(FileName,3))="png" then
Response.ContentType = "image/*" '对图像文件不出现下载对话框
else
Response.ContentType = "application/ms-download"
end if
Response.AddHeader "content-disposition", "attachment; filename=" & GetFileName(Request.QueryString("FileName"))
Set Stream = server.CreateObject("ADODB.Stream")
Stream.Type = adTypeBinary
Stream.Open
if lcase(right(FileName,3))="pdf" then '设置pdf类型文件目录
TrueFileName = "/the_pdf_file_s/"&FileName
end if
if lcase(right(FileName,3))="doc" then '设置DOC类型文件目录
TrueFileName = "/my_D_O_C_file/"&FileName
end if
if lcase(right(FileName,3))="gif" or lcase(right(FileName,3))="jpg" or lcase(right(FileName,3))="png" then
TrueFileName = "/all_images_/"&FileName '设置图像文件目录
end if
Stream.LoadFromFile Server.MapPath(TrueFileName)
While Not Stream.EOS
Response.BinaryWrite Stream.Read(1024 * 64)
Wend
Stream.Close
Set Stream = Nothing
Response.Flush
Response.End
%>
本地图片,音乐等ASP防盗链代码(asp)

以下是引用片段:

以下是引用片段:
<%
'定义函数,用ADODB.Stream读取二进制数据
Function ReadBinaryFile(FileName)
Const adTypeBinary = 1
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = adTypeBinary
BinaryStream.Open
BinaryStream.LoadFromFile FileName
ReadBinaryFile = BinaryStream.Read
End Function

Response.AddHeader "Content-Disposition", "attachment;filename=2.gif"'文件名
Response.ContentType = "image/GIF" ’设置(1)
response.Binarywrite ReadBinaryFile(server.mappath("2.gif"))'就是你读取存在本地的文件,防止被
别人知道真实路径盗连的。

%>
(1)下面的示例将 ContentType 属性设置为其他的常见值。
text/HTML 这个就不说了
image/GIF gif图片
image/JPEG jpg图片
application/x-cdf cdf文档
application/wma 就是西瓜哪个音乐类型了
具体可以参照 Web 浏览器文档或当前的 HTTP 规格说明

这样再利用asp的储存session,cookies,以及读取HTTP头等特殊功能就可以完全真正的实现防盗连,这里
没有设置缓存,如果访问量巨大,我想设置下就会更好吧。

asp下载防盗链代码
第一种:
终于对下载系统做了个防盗链措施,在下载的页面头部做了如下代码,相关代码如下:
以下是引用片段:

以下是引用片段:
<%
From_url = Cstr(Request.ServerVariables("HTTP_REFERER"))
Serv_url = Cstr(Request.ServerVariables("SERVER_NAME"))
if mid(From_url,8,len(Serv_url)) <> Serv_url and mid(From_url,8,len(Serv_url))<>"ITstudy.cn" and mid(From_url,8,len(Serv_url))<>"www.gc888.cn" then
response.write "您下载的软件来自IT学习网,请直接从主页下载,谢谢<br>" ’防止盗链
response.write "<a href=http://www.gc888.cn>IT学习网http://www.gc888.cn</a>" ’防止盗链
response.end
end if
%>
第二种:
以下是引用片段:

以下是引用片段:
<%
’定义函数,用ADODB.Stream读取二进制数据
Function ReadBinaryFile(FileName)
Const adTypeBinary = 1
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = adTypeBinary
BinaryStream.Open
BinaryStream.LoadFromFile FileName
ReadBinaryFile = BinaryStream.Read
End Function

Response.AddHeader "Content-Disposition", "attachment;filename=2.gif"’文件名
Response.ContentType = "image/GIF" ’设置(1)
response.Binarywrite ReadBinaryFile(server.mappath("2.gif"))’就是你读取存在本地的文件,防止被
别人知道真实路径盗连的。

%>
下面的示例将 ContentType 属性设置为其他的常见值。

text/HTML 这个就不说了
image/GIF gif图片
image/JPEG jpg图片
application/x-cdf cdf文档
application/wma 就是西瓜哪个音乐类型了
具体可以参照 Web 浏览器文档或当前的 HTTP 规格说明

这样再利用asp的储存session,cookies,以及读取HTTP头等特殊功能就可以完全真正的实现防盗连,这里
没有设置缓存,如果访问量巨大,我想设置下就会更好吧。

第三种:
最简单的用Active Server Pages防站外提交表单、跨站提交表单、防盗链……

方法:Request.SeverVariables("HTTP_REFERER")
解释:当某人通过链接到达当前页,HTTP_REFERER 就保存了这个用户的来源(来路)

举个例子,这个例子很简单,只是抛砖引玉而已,大家可以增加更多的功能。
如下,只有首先从“ http://www.gc888.cn”登陆才能看到文件内容。

源码:index.asp

以下是引用片段:

以下是引用片段:
<html>
<head><title>最简单的用asp防盗链</title></head>
<body>
<%
Option.Explicit
Response.Buffer=Ture
%>
<%
CheckUrl(http://www.gc888.cn)
%>
<%
Function CheckUrl(url)
Dim Where:Where=Request.SeverVariables("HTTP_REFERER")
If Where=url Then
Call main()
Else
Response.write("很抱歉,您必须从"&url&"访问才能进来!")
End if
End Function
%>
<%
Sub main()
Response.write("这儿是你要显示的网页内容")
End sub
%>
</body>
</html>
该方法对防止盗链文章、站外提交表单、跨站提交表单还比较有效,对于软件盗链比如.rar.zip.exe等倒没什么作用。
不知各位读者是否有好的主意,呵呵。

还有一种方法就是用判断服务器及上一页的地址来完成。
以下是引用片段:

以下是引用片段:
<%
dim from, local
from = request.ServerVariables("HTTP_REFERER")
local = request.ServerVariables("SERVER_NAME")
If mid(from, 8, local)<>Len(local) Then
response.write "不要从外部提交数据"
else
call main()
end if
sub main()
’你的主体内容
end sub
%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: