Jump to content

PHP show image if pulled from specific URL?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
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.
Checkout my new forum! http://adminreference.com/

#2
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Got it..

<?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
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
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