pratie

ASP/VBScript to prevent image leeching

Published: a long time ago

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 leech images from your 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?img=wall01.jpg).

<%
response.buffer = true

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

Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")

if instr(ref, lcase("mysite.com"))>0 then 
   
	objStream.Type = 1
	objStream.Open
	objStream.LoadFromFile "d:\webhost\mysite\images\download\" & img
	Response.ContentType = "image/JPEG"
	Response.AddHeader "content-disposition", "inline;filename=" & img
	Response.BinaryWrite objStream.Read

else

	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

objStream.Close
Set objStream = Nothing
%>