So I thought this would be an easy concept.. But I guess not.
Lets say I have a php file, image.php, and it basically displays an image, so you can just..
<img src="http://website.com/images/image.php"> and it will show the image defined in the PHP code, no big deal...
Anyone know of an easy way to make it ONLY show if the image tag is on a specific website?.. like make it so it will only show on allowedwebsite.com, and not somesite.com? Cant restrict hotlinking all together, because I kinda wanna make it so they can whitelist domains to pull the images.
2 replies to this topic
#1
Posted 07 April 2011 - 01:40 AM
Checkout my new forum! http://adminreference.com/
|
|
|
#2
Posted 07 April 2011 - 02:02 AM
Got it..
I always seem to post on here, then figure out my own problem, just moments later, lol
<?php
header("Content-type: image/png");
$referrer = $_SERVER['HTTP_REFERER'];
if(!preg_match("/^http:\/\/somewebsite.com/i", $referrer))
{
$string = "Unable to view this image from " . $referrer;
$font = 4;
$width = imagefontwidth($font) * strlen($string);
$height = imagefontheight($font);
$image = imagecreatetruecolor ($width,$height);
$white = imagecolorallocate ($image,255,255,255);
$black = imagecolorallocate ($image,0,0,0);
imagefill($image,0,0,$white);
imagestring ($image,$font,0,0,$string,$black);
imagepng ($image);
imagedestroy($image);
}
else
{
$imagepath="google.png";
$image=imagecreatefrompng($imagepath);
imagepng($image);
}
?>
I always seem to post on here, then figure out my own problem, just moments later, lol
Checkout my new forum! http://adminreference.com/
#3
Posted 07 April 2011 - 10:59 AM
You'd have to test every URL that way though. I threw a little program together:
<?PHP
$subDomainPattern = "/^https?:\/\/([^\/]+)/i";
$domainPattern = "/^https?:\/\/([^\.]+\.)?([^\.]+\.[^\/]+)/i";
$safeSites = array('mail.google.com', 'codecall.net');
$sites = array('http://us2.php.net/preg_match_all','https://mail.google.com/mail/','http://forum.codecall.net','http://google.com/firefox');
echo "<pre>";
foreach($sites AS $referrer) {
// Matches domain + subdomain
preg_match_all($subDomainPattern, $referrer, $matches);
if(in_array($matches[1][0], $safeSites)) {
echo "<span style='color:green'>G: {$matches[1][0]}</span>\n"; // mail.google.com
} else {
echo "<span style='color:red'>B: {$matches[1][0]}</span>\n";
}
}
echo "\n\n";
foreach($sites AS $referrer) {
// Matches domain
preg_match_all($domainPattern, $referrer, $matches);
if(in_array($matches[2][0], $safeSites)) {
echo "<span style='color:green'>G: {$matches[2][0]}</span>\n"; // codecall.net
} else {
echo "<span style='color:red'>B: {$matches[2][0]}</span>\n";
}
}
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top










