PDA

View Full Version : showing bandwidth stealers an alternate image/file


bturner
04-29-2002, 02:25 PM
I seem to recall a thread on this board (it could have been quite a while ago), that described a way to use .htaccess to display an alternative image to bandwidth stealers.

For example, they were trying to hard link to a photo on your site, but when they put the code on their page, instead of seeing the actual image, they get an alternate that says something like "I steal bandwidth" or something equally as witty.

I've read the thread on how to stop the stealing, but can't find the one for the above solution.

Any ideas? Thanks!

Bill

kitchin
04-29-2002, 05:11 PM
Here it is if you are willing to rename your file from .htm to .php:

<?php
// comment out after testing:
print "<br>test: ra [$REMOTE_ADDR]<br>\n";

$img= 'myspecialpicture.jpg';

// as many as you want:
$badblock[]= '123.123.123.';
$badblock[]= '12.12.12.';

foreach ($badblock as $bad) {
$p= strpos($REMOTE_ADDR, $bad);
if ($p === 0) {
$img= 'myexasperatedpicture.gif';
break;
}
}
?>

blah<br>
<img src="<?php echo $img ?>" width=100 height=120 border=1><br>
bleh<br>

If you need the version that matches exact IP's, tell me.

bturner
04-29-2002, 05:28 PM
Actually, the solution I was looking for involved using htaccess and the referer matching rules. Similar to how you can block bandwidth stealers at this forum thread:

http://www.aota.net/forums/showthread.php?s=&threadid=10255

Thanks
Bill

kitchin
04-29-2002, 08:18 PM
Mind if I post a guess? I think the answer goes something like:
To offer alternate content instead of blocking requires mod_rewrite, which has a fairly heavy overhead and must be done verrrry carefully. PHP is simpler to use and less heat on the server.
How's that for a guess!

kitchin
04-29-2002, 08:30 PM
And you could change it so it could be used like this:
<img src="getimage.php" width=100 height=120>
in any file.

In this setup, getimage.php would only output

<?php
// ... as above
header("Location: /$img");
exit;
?>

Make sure <?php is pushed up against the top of the file.

bturner
04-30-2002, 11:28 AM
kitchin,

Thanks for the tips, but none of the options you give me will really do what I need. In both the cases you show, once someone finds where the image is stored on the webserver, there's nothing to keep them from pointing to it.

You may be right about the mod_rewrite, but I could swear that there was a non-mod_rewrite htaccess method of doing this.

bill

kitchin
05-01-2002, 09:33 AM
Oh, do you mean the thief has put something like
<img src="http://yourdomain.com/pic.gif">
on his web site? The recent thread about that is here:
http://www.aota.net/forums/showthread.php?s=&threadid=11205
which mentions the Thudfactor's FAQ on how to use .htaccess to stop it:
http://www.thudfactor.com/writing.php?show=18

bturner
05-01-2002, 12:40 PM
Yeah, I saw those threads. I was thinking there was another, maybe in the old forum, that went one step further and redirected any requests to an alternate image.

bill

kitchin
05-01-2002, 01:26 PM
Call this file "pic.php":

<?php
$img= 'myexasperatedpicture.gif'; // reject picture
$goodimg= 'myspecialpicture.jpg'; // good picture

$goodref[]= 'http://okdomain.tld/';
// ... as many as you want, best to use trailing /
$goodref[]= 'http://www.okdomain.tld/';

// nothing below here needs to change
if ($ref = strtolower($HTTP_REFERER)) {
foreach ($goodref as $good) {
if ( strpos($ref, $good) === 0 ) {
$img= $goodimg;
break;
}
}
}
else {
// Your choice whether to allow calls without referer.
// Uncomment this if so:
// $img= $goodimg;
}

header("Location: $img");
exit; // just to be sure
?>


Then use <img src="pic.php"> in your html.

Note, I've seen a lot of weird things in where the "www." goes in "http://www.okdomain.tld/" including "ftp." and something with an "@". You can look at your logs to see if the above misses much. The last entry is the referer, if I recall. The script is lean as is; add regexs if you prefer.
/^http\:\/\/([^\/\%]*\.)?okdomain\.tld\//

bturner
05-01-2002, 04:36 PM
Sorry, but this won't help. I don't use PHP on the site, and it still doesn't really stop someone from deep linking to an image on my server. I'm looking for an htaccess approach.

Thanks,
Bill

kitchin
05-01-2002, 06:57 PM
Only requires one php file per picture, the html can stay html.
But you're right, they could still find the ".gif" name fairly easily.
Maybe the script could ouput the bits instead of the "location," but I don't know.

The answer must be mod_rewrite.
The Apache web page
http://httpd.apache.org/docs/misc/rewriteguide.html
has a very similiar example to what you are looking for (scroll way down to "Blocked Inline-Images"), although it may be missing "RewriteEngine on" and it is really two examples.

<edit>

You would change
RewriteRule .*\.gif$ - [F]
to
RewriteRule ^[^z].*\.gif$ zbad.gif
I believe.
The ^[^z] is only there in case you have
to worry about circular references??
Only works on images not starting with z.

</edit>