Zend - The PHP Company




Graphics

Add Code


Hotlinking Image Protection  

Type: code fragment
Added by: xpc
Entered: 14/03/2003
Last modified: 03/12/2002
Rating: - (fewer than 3 votes)
Views: 6122
Hotlinking protection support from PHP: This simple script is an easy way to protect your media images or any files for that matter from remote linking which leads to bandwidth theft.


<?

 
/***

* 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' );

$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 != ){

        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: /' ) );

}

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories