ASP simple anti-leech

Source: Internet
Author: User
Tags servervariables
If we know the actual path of a static file, for example, success! How can I prevent the downloader from obtaining the actual path when downloading 51windows.pdf on the website! This article describes how to use ASP to hide the actual download path of an object.

When we manage website files, we can place files with the same extension in the same directory and create a special name. For example, we can save the following code as down. ASP. Its online path is we can use 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.

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" 'requires logon!
End if
Function getfilename (longname) '/folder1/folder2/file. asp => file. asp
While instr (longname ,"/")
Longname = right (longname, Len (longname)-1)
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 file name! "
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.
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' 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)
Stream. Close
Set stream = nothing
Response. Flush
Response. End

From: Id = 512

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: 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.