ASP/VBSCript to Prevent Image Leeching

This code can be used to prevent other websites from leeching images from your site. This is useful if you are hosting large images such as wallpaper/desktops. Other sites sometimes directly display images from your site on their site by using an img tag with an absolute path such as: <img src="http://www.yoursite.com/images/wall01.jpg" />. This code prevents that from happening.

Create a hidden directory on your website and place your images in it. Modify the directory location in the script accordingly. Then link to a page containing the script and pass the image file name as a variable (default.asp?desktop=wall01.jpg).

<%
response.buffer = true

ref = lcase(Request.ServerVariables("HTTP_REFERER"))
desktop = trim(Request.QueryString("desktop"))

'create a stream object
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")

if instr(ref, lcase("mysite.com"))>0 then 
   
 'Open file
 objStream.Type = 1 'adtypeBinary
 objStream.Open
 objStream.LoadFromFile "d:\webhost\mysite\images\download\" & desktop
 'output the contents of the stream object
 Response.ContentType = "image/JPEG"
 'Content-Disposition", "attachment; -- prompts user to open or save
 'Content-Disposition", "inline; -- file opens through browser window
 Response.AddHeader "content-disposition", "inline;filename=" & desktop
 Response.BinaryWrite objStream.Read

else 'write out leech warning image

 objStream.Type = 1
 objStream.Open
 objStream.LoadFromFile "d:\webhost\mysite\images\leech.jpg"
 Response.ContentType = "image/JPEG"
 Response.AddHeader "content-disposition", "inline;filename=leech.jpg"
 Response.BinaryWrite objStream.Read

end if

'clean up..
objStream.Close
Set objStream = Nothing
%>