Page 1 of 1

Hotlinking?

Posted: Tue Apr 26, 2005 8:56 pm
by Todd_Z
How do I check if an image is being hotlinked from my site... Basically I am making a script which watermarks my images if they aren't being read on my webpage.

Posted: Tue Apr 26, 2005 9:00 pm
by John Cartwright
look at

Code: Select all

echo '<pre>'. print_r($_SERVER) .'</pre>';
and then create a function that will compare some of your values, such as SERVER_ADDR and make sure it is the same as your servers. This will require you keep all your images in a directory perhaps outside your web path, and require a script to call your images for it to be displayed.

Posted: Tue Apr 26, 2005 9:05 pm
by Todd_Z
But if that is within a script, for example, people who hotlink would use something like this-

<img src="http://www.acdrifter.com/Watermark.php?pic=LKA2154asds" alt="Movie Title" />

Then wouldn't the server values still be my server... not the one calling for the image?

Posted: Tue Apr 26, 2005 9:26 pm
by John Cartwright
They will never know that any script is being used to call the image as the script will output the image along with the image headers.. Perhaps include a specifc key that is also required when calling the script. Or Perhaps a session created on your index must exist for the script to run.

Posted: Tue Apr 26, 2005 10:41 pm
by Todd_Z
Ok, well I don't really want any more variables involved, and i'm refraining from using sessions... any other ideas?

Posted: Tue Apr 26, 2005 11:57 pm
by John Cartwright
Whats wrong with sessions?

Posted: Wed Apr 27, 2005 5:18 am
by onion2k
Mark a .htaccess file that detects if the referer is your website:

Code: Select all

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^$ 
RewriteCond %{HTTP_REFERER} !^http://www.mydomain.com.*$ &#1111;NC] 
RewriteRule (.*)&#1111;Jj]&#1111;Pp]&#1111;Gg]$|(.*)&#1111;Gg]&#1111;Ii]&#1111;Ff]$|(.*)&#1111;Pp]&#1111;Nn]&#1111;Gg]$ watermark.php?image=$1
Then make watermark.php open the requested image and watermark it.

Code: Select all

<?php
$image = imagecreatefromjpg($_GET['image']);
$wm = imagecreatefrompng("watermark.png");

imagecopy($image,$wm,0,0,0,0,50,50);

header("Content-type: image/jpeg");
imagejpeg($image);
?>
Obviously you'll need to expand that code to deal with other image types, and you may want to tweak it to calculate the bottom right corner or the middle or something to place the watermark. But thats roughly the sort of thing you'll need.

None of this is tested by the way. So the code is more of a pointer in the right direction that a cut'n'paste solution.

Posted: Wed Apr 27, 2005 1:29 pm
by Todd_Z

Code: Select all

RewriteEngine on
RewriteLog &quote;*********/************/**************/Logs/Hotlinking.log&quote;
RewriteLogLevel 1
RewriteCond %{HTTP_REFERER} !^$ 
RewriteCond %{HTTP_REFERER} !^http://www.acdrifter.com.*$ &#1111;NC] 
RewriteRule (.*)&#1111;Jj]&#1111;Pp]&#1111;Gg]$ Watermark.php?pic=$1
This causes an Error code 500 on my entire site...

What's wrong?!

My guess -
There is nothing in the condition about the file being a jpg, and I don't really understand the importance of 'RewriteCond %{HTTP_REFERER} !^$'

Maybe???

Code: Select all

RewriteCond %{HTTP_REFERER} !^http://www.acdrifter.com/ &#1111;NC]
RewriteCond %{HTTP_REFERER} .jpg$ &#1111;NC]

Posted: Wed Apr 27, 2005 2:22 pm
by Todd_Z
After much mucking around -

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://www.acdrifter.com/ [NC]
RewriteRule (.*)jpg$ Watermark.php?pic=$1jpg

This works. Yay.

Posted: Wed Apr 27, 2005 2:43 pm
by John Cartwright
I guess it truly is time to find out the full extent of mod rewrite ;0

Posted: Wed Apr 27, 2005 2:49 pm
by Todd_Z
It's a beautiful thing - I was just checking it out today. The abilities are unbelievable. For example instead of...

http://www.acdrifter.com/?page=Review&m ... 0of%20Fury

You can do

http://www.acdrifter.com/Review/House of Fury/

then rewrite it back to the original...

idk why, but I find that intriguing

Posted: Wed Apr 27, 2005 2:52 pm
by John Cartwright
Oh don't get me wrong, I frequently use to it make my links search engine friendly and such. I guess I never thought of it in a different kind of application.

Posted: Thu Apr 28, 2005 4:56 am
by onion2k
Tiz most handy.