选择显示字体大小

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


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons