对于一些下载站来说,最郁闷的就是要下载的文件被人知道了地址而被盗链。你想象一下,你有一个下载站,你好不容易收集来大量的软件,并且提供了大量的服务器和带宽来支撑用户的下载。我也做一个跟你一模一样的下载站,但我没有软件,我的网站上点下载时,直接链接到你的下载文件地址。那我虽然太卑鄙了,可你的损失的确是太大了!
好了,那我们来看看在asp中如何隐藏文件路径来防止被他站盗链,简称防盗链。
FunctIon DownloadFIle(strFile)
strFileName=strFile
Response.Buffer=True
Response.Clear
Set S=Server.CreateObJect("ADODB.Stream")
S.Open
S.Type=1
on Error Resume Next
Set Fso=Server.CreateObJect("ScrIptIng.FIleSystemObJect")
If Not Fso.FIleExists(strFileName) Then
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
Response.RedIrect Request.ServerVarIables("HTTP_REFERER")
Response.End
End If
FileExt=MId(strFileName,InStrRev(strFileName, ".")+1)
Select Case UCase(FIleExt)
Case "ASP", "ASA", "ASPX", "ASAX", "MDB", "PHP", "JSP", "SHTML", "HTML", "HTM", "TV", "DATA"
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
Response.RedIrect Request.ServerVarIables("HTTP_REFERER")
Response.End
End Select
Set F=Fso.GetFIle(strFileName)
IntFIlelength=F.SIze
s.LoadFromFIle(strFileName)
If Err Then
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
Response.RedIrect Request.ServerVarIables("HTTP_REFERER")
Response.End
End If
Set Upload=Server.CreateObJect("PersIts.Upload")
If Upload Is Nothing Then
Response.AddHeader "Content-DIsposItIon","attachment; FIlename="&F.Name
Response.AddHeader "Content-Length",IntFilelength
Response.CharSet="UTF-8"
Response.ContentType="application/x-download"
Response.BinaryWrite S.Read
Response.Flush
S.Close
Set s=NothIng
Else
Upload.SendBinary strFileName,True,"application/x-download",False
End If
End FunctIon
%>
这个防盗链函数的调用方式如下:
<%Call DownloadFile("下载地址")%>
注意,这里的下载地址,Server.MapPath映射过后的真实地址!
关于ASP隐藏文件路径以防盗链,本文就介绍这么多,希望对您有所帮助,谢谢!