林网博客

探讨计算机网络安全 拓展计算机网络应用

« 小榕是英雄还是败类真真假假的svchost占用率过高 »

ASP简单防盗链方法

如果我们知道一个静态文件的实际路径如:http: //www.xx.com/download/51windows.pdf,如果服务器没有作特别的限制设置,我们就可以毫不费力的把它下载下来!当网站 提供51windows.pdf下载时,怎么样才能让下载者无法得到他的实际路径呢!本文就来介绍如何使用Asp来隐藏文件的实际下载路径。

我们在管理网站文件时,可以把扩展名一样的文件放在同一个目录下,起一个比较特别名字,例如放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
%>





本文链接地址:http://www.linwan.net.cn/archives/2346.html
原创文章如转载,请注明:转载自林网博客 [ http://www.linwan.net.cn/ ]

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

订阅博客

  • 订阅我的博客:订阅我的博客
  • 通过Google订阅本站
  • 通过抓虾订阅本站
  • 通过鲜果订阅本站
  • 通过bloglines订阅本站
  • 订阅本站到飞豆
  • 订阅本站到哪吒
  • 订阅到有道阅读
  • 通过电子邮件订阅本站

林海谐缘软件区新贴

林海谐缘网络区新贴

联系邮箱:我的信箱地址,Powered By Z-Blog 1.8 Spirit Build 80710

本站采用创作共用版权协议, 要求署名、非商业用途和保持一致. 转载本站内容必须也遵循“署名-非商业用途-保持一致”的创作共用协议.