PHP : Hotlinking Image Protection

3 Mar

<?

/***
*
* PHP Hotlinking protection by Michael Glazer http://coding.4arrow.com March 13, 2003
*
* Let’s Roll!
* America Rules!
*
* Sample Usage:
*
* <img src=”image.php?myLogo.gif” />
*
*
* You can freely use this script as long as this header remains intact
*
**/

$allowed = array ( ‘4arrow.com’ , ‘montros.org’ , ‘phportal.org’, ‘swifthost.net’);

$ref = $_SERVER[‘HTTP_REFERER’];

$imagedir = ‘/HiddenPathTo/MyImages/’;

$image = $argv[0];

$g = 0;

if ( $ref != ” && $image != ” ) {

foreach ( $arr as $u ) {

if ( strpos ( $ref , $u ) !== false ) {

$g=1;
break;

}

}

if ( $g != 1 ){

die ( Header( ‘HTTP/1.0 403 Forbidden’ ) );

} else {

// load image

$imagepath = $imagedir . $image;

$imageinfo = getimagesize ( $imagepath );

$fp = fopen ( $imagepath , ‘rb’ );

if ( $imageinfo && $fp ) {

header ( ‘Content-Type: ‘ . $imageinfo[‘mime’] );

die ( fpassthru ( $fp ) );

} else {

die ( Header ( ‘HTTP/1.0 404 Not Found’ ) );

}

}

} else {

// direct call redirect to root

die ( header ( ‘Location: /’ ) );

}

?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: