asp下載防盜鏈代碼
第一種:
終於對下載系統做了個防盜鏈措施,在下載的頁面頭部做瞭如下代碼,相關代碼如下:
<% From_url=Cstr(Request.ServerVariables("HTTP_REFERER"))
Serv_url=Cstr(Request.ServerVariables("SERVER_NAME"))
ifmid(From_url,8,len(Serv_url))<>Serv_urlandmid(From_url,8,len(Serv_url))<>"ITstudy.cn"andmid(From_url,8,len(Serv_url))<>"www.ITstudy.cn"then
response.write"您下載的軟件來自IT學習網,請直接從主頁下載,謝謝<br>"'防止盜鏈
response.write"<ahref=http://www.ITstudy.cn>IT學習網http://www.ITstudy.cn</a>"'防止盜鏈
response.end
endif
%>
第二種:
<% '定義函數,用ADODB.Stream讀取二進制數據
FunctionReadBinaryFile(FileName)
ConstadTypeBinary=1
DimBinaryStream
SetBinaryStream=CreateObject("ADODB.Stream")
BinaryStream.Type=adTypeBinary
BinaryStream.Open
BinaryStream.LoadFromFileFileName
ReadBinaryFile=BinaryStream.Read
EndFunction
Response.AddHeader"Content-Disposition","attachment;filename=2.gif"'文件名
Response.ContentType="image/GIF"'設置(1)
response.BinarywriteReadBinaryFile(server.mappath("2.gif"))'就是你讀取存在本地的文件,防止被
別人知道真實路徑盜連的。
%>
(1)下面的示例將ContentType屬性設置為其他的常見值。
text/HTML這個就不說了
image/GIFgif圖片
image/JPEGjpg圖片
application/x-cdfcdf文檔
application/wma就是西瓜哪個音樂類型了
具體可以參照Web瀏覽器文檔或當前的HTTP規格說明
這樣再利用asp的儲存session,cookies,以及讀取HTTP頭等特殊功能就可以完全真正的實現防盜連,這裡
沒有設置緩存,如果訪問量巨大,我想設置下就會更好吧。