Forum Moderators: coopster

Message Too Old, No Replies

Hotlinking prevention,Php or .htaccess?

         

moroose

9:31 am on Aug 15, 2007 (gmt 0)

10+ Year Member



Hi all,
I'd be thankful to anyone who can clarify to me whether hotlinking prevention can be done solely thru php?

If yes,I appreciate a simple illustration example

thank you for any help

PHP_Chimp

5:19 pm on Aug 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Seeing as no one else has had a go at this I'll try...Always up for a challenge.
The reason that im guessing no one else has tried is that it would be a lot easier through htaccess.

Yes you could do it through PHP. HOWEVER there is a big problem in that you would have to pass ALL requests through a php script that would then look for .jpg, .gif whatever file extensions you would like to prevent getting linked.

So you would need to have a 'gateway' page that had nothing but your processing script. You would then need to use something like the query string to send the request from the gateway to the actual location of the page you were after.
i.e.
'gateway' page - so could be the index page

So request would be something like - yoursite.com/?important_page.php

<?php
$r = $_SERVER['REQUEST_URL'];
$pat = "%\?(.*)\.(jpe?g¦gif¦tiff¦bmp)$%"
$image = preg_match($patt, $r);

if ($image!= 0) { // pattern found
$header = 'Location: dont_steal_my_pics';
}
else {
$not_image = explode('?', $r);
$header = "Location: $not_image";
}
?>

Iv just made that up so dont rely on it until it has been tested...lots lol.

The problem with this is that seeing as you are putting the actual location of whatever you really want in the query string it wont take a brain surgeon to work out what the actual location of all the images are and bypass your filtering. So you could try something 'cunning' like crypt'ing all of the pages then putting that as the query string and decoding that as part of the 'gateway'.

You could of course use htaccess to rewrite all requests to go through this script...but then why not use htaccess to prevent the hotlinking :D

Personally I quite enjoy finding the idiots that hotlink, as you use htaccess to redirect them to something (in)appropriate...like an porn site when it is a local council hotlinking your images (oops, my bad), or put some XSS that redirects there visitors to your site.
I have no problem with people hotlinking...as if they want to steal my images then they will have to put up with whatever I decide to do to there visitors >:) What are they going to do, take you to court for them stealing your bandwidth?

moroose

6:00 pm on Aug 18, 2007 (gmt 0)

10+ Year Member



Excellent!
Thank you very much PHP_Chimp and I command your "up to the challenge"
Yes,your response was both informative and entertaining
Actully your php concept worked for me initially.As am up for challenge,too,am striving to get htaccess join in the fun as well.
But as you said,htaccess can do it alone(though exhausting the server)as i use it in many instances for other stuff.
My gratitude for your stamina to write that long to explain to me :)