Use ASP to hide file paths and implement anti-leech Protection

Source: Internet
Author: User

When we manage website files, we can put files with the same extension under the same directory and create a special name, for example, put the PDF file directory as the_polic_file_s.CodeSave it as "down. asp". Its online path is http://www.xx.com/down.asp. we can use http://www.xx.com/down.asp? Filenameapps51windows.pdf to download this file, and the downloader cannot see the actual download path of this file! In down. asp, you can also set whether to log on to the downloaded file to determine whether the downloaded Source Page is an external website, so as to prevent the file from being leeched.[Source: feitec.com content, reprinted, please specify the source]

Sample Code:[Source: feitec.com content, reprinted, please specify the source]

<%
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 "illegal link! "'Preventing leeching
response. end
end if
If request. cookies ("logined") = "" Then
response. redirect "/login. ASP "'login required!
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 "invalid/Webpage/asp/041101/24 .htm! "
Response. End
End if

Fileext = mid (filename, limit Rev (filename, ".") + 1)
Select case ucase (fileext)
Case "asp", "asa", "aspx", "asax", "mdb"
Response. Write "illegal operation! "
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/*" 'does not display the download dialog box for the image file.
Else
Response. contenttype = "application/MS-download"
End if

Response. addheader "content-disposition", "attachment; 200497141230.htm=" & getfilename (request. querystring ("FILENAME "))
Set stream = server. Createobject ("ADODB. Stream ")
Stream. type = adtypebinary
Stream. Open
If lcase (right (filename, 3) = "pdf" then' sets the PDF file directory
Truefilename = "/the_polic_file_s/" & filename
End if
If lcase (right (filename, 3) = "Doc" then' sets the doc-type file directory
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 'set the image file directory
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
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.