当前位置: 首页 > 图文教程 > 网络编程 > ASP > 用Asp隐藏文件路径,实现防盗链 的代码

ASP
ASP下经常用的字符串等函数参考资料
asp下连接数据库 ASP链接数据库字符串大全总结
asp内置对象 ObjectContext 事务管理 详解
asp 内置对象 Application 详解
ASP中 SQL语句 使用方法
ASP 中 Split 函数的实例分析
ASP 中 DateDiff 函数详解 主要实现两日期加减操作
asp 存贮过程 (SQL版asp调用存储过程)
利用ASP实现在线生成电话图片效果脚本附演示
使用ASP记录在线用户的数量的代码
关于ASP生成伪参数技巧 简洁实用的伪(僞)参数
asp 关键词字符串分割如何实现方法
用ASP编写的加密和解密类
ASP之处理用Javascript动态添加的表单元素数据的代码
asp下最常用的19个基本技巧
一些Asp技巧和实用解决方法
asp实现一个统计当前在线用户的解决方案
asp下的一个很简单的验证码程序
asp又一个分页的代码例子
asp实现防止从外部提交数据的三种方法

ASP 中的 用Asp隐藏文件路径,实现防盗链 的代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 24 ::
收藏到网摘: n/a

用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
%>