防盜鏈技術在ASP使用

來源:互聯網
上載者:User
防盜鏈

如果我們知道一個靜態檔案的實際路徑如: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.QueryStrin­g("FileName"))
Set Stream = server.CreateObject("ADODB.Str­eam")
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
%>

利用adodb.stream直接下載任何尾碼的檔案(防盜鏈)

在瀏覽器的地址欄裡直接輸入一個doc或xls或jpg的檔案的­url路徑,那麼該檔案會直接顯示在瀏覽器裡。而在很多時候我們­希望能直接彈出下載提示框讓使用者下載,我們該怎麼辦呢?這裡有兩­種方法:

1、設定你的伺服器的iis,給doc等尾碼名做映射
2、在向用戶端發送時設定其contenttype

[1] [2] 下一頁  



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.