ASP hotlink prevention has always been a headache for friends who make websites for advertising. Hotlinking seriously affects the normal operation of servers and websites. It has become very necessary to release hotlinks. Today, let’s take a look at a method to implement ASP hotlink prevention. Bar.
If we know the actual path of a static file such as: http://www.xx.com/download/webjx.pdf, and if the server does not set any special restrictions, we can download it effortlessly! When the website provides webjx.pdf download, how can we prevent the downloader from getting his actual path? This article will introduce how to use Asp to hide the actual download path of files.
When we manage website files, we can put files with the same extension in the same directory and give them a special name. For example, the directory where the pdf files are placed is the_pdf_file_s, and the following code is saved as down.asp, and its online path is http. ://www.xx.com/down.asp, we can use http://www.xx.com/down.asp?FileName=webjx.pdf to download this file, and the downloader cannot see this file The actual download path! In down.asp, we can also set whether downloading files requires logging in, and determine whether the downloaded source page is an external website, thus preventing files from being linked.
<% 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! ''Prevent hotlinking response.end end if if Request.Cookies(Logined)= then response.redirect /login.asp ''Requires login! 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 file name! Response.End End if FileExt = Mid(FileName, InStrRev(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/* ''No download dialog box appears for image files else 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 ''Set pdf type file directory TrueFileName = /the_pdf_file_s/&FileName end if if lcase(right(FileName,3))=doc then ''Set 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 %> |