喜欢本站并加入收藏
首 页 电脑学园 技术文档 图形图像 办公应用 网页设计 管理资料 PPT模板 方案合同
 位置: 北方教程网 >> 网页设计 >> Asp专题 >> 正文
   ASP防止盗链和防止下载的技巧
ASP防止盗链和防止下载的技巧
[ 作者:佚名   来源:网络整理   点击数:   更新时间:2007-3-1 ]

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

[1] [2] [3]  下一页

上一篇文章: ASP远程数据获取程序入门教程 下一篇文章: 运用ASP调用数据库中视图及存储过程原理
打印此文  收藏此页  关闭窗口  返回顶部
 相关文章  热点文章
 · 利用CSS改进网站设计的小技巧
 · Dreamweaver快捷操作技巧集锦
 · 使用Dreamweaver架设网站简明教程
 · 网页设计制作过程中常用的几个代码技巧
 · 高效使用CSS编码的方法
 
热点图文
教你节省Vist...
使用Windows ...
 · 远程管理Web服务器的方法技巧
 · Win Vista系统自带IIS7.0设置详解...
 · 详解Windows系统IIS6.0功能及应用...
 · 服务器集群系统实现方法及案例分...
 · IIS服务器实现asp,cgi和php+mysq...
 · IIS下SQL Server数据库安全设置技...
推 荐 阅 读
巧用Photosho...
Photoshop鼠绘...
 · Fireworks制作旋转的漂亮gif动画...
 · Photoshop制作都市满天繁星
 · Fireworks制作Menu按钮
 · 巧用Photoshop制作喜迎奥运壁纸
 · 巧用Photoshop钢笔工具绘制彩色美...
 · Photoshop制作浪漫动感花朵
 设为首页 - 加入收藏 - 关于本站 - 联系我们 - 广告合作 - 友情链接 - 在线交流 - 
CopyRight© 2006-2009 Eduai.Com All Rights Reserved
在线交流 QQ:524152258 辽ICP备07001851号
免责声明:本站部分资源来自网络,如有侵犯您的版权请通知,我们立即删除。