Hotlinking?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Hotlinking?

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Ok, well I don't really want any more variables involved, and i'm refraining from using sessions... any other ideas?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Whats wrong with sessions?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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]
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I guess it truly is time to find out the full extent of mod rewrite ;0
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Tiz most handy.
Post Reply