当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  用Asp隐藏文件路径实现防盗链

 用Asp隐藏文件路径实现防盗链

点击次数:16 次 发布日期:2008-11-26 11:11:00 作者:源代码网
源代码网推荐      如果我们知道一个静态文件的实际路径如: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
源代码网推荐  %>
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华