PHP to prevent remote linking of images: Difference between revisions

From Robupixipedia
Jump to navigationJump to search
(initial version; hasn't been tested since I stripped it from my code at work)
 
(wiped out a block of crap that no one will need)
 
Line 6: Line 6:
== Caveats ==
== Caveats ==


I am just slapping this code down here so you can pick out the bits you like.  I've stripped most of the code that's specific to my environment, but haven't tested it since then.
I am just slapping this code down here so you can pick out the bits you like.  I've stripped most of the code that's specific to my (work) environment, but haven't tested it since then.


== Source ==
== Source ==
Line 20: Line 20:
$image_list = array('good' => 'goodimage.gif', 'bad' => 'bad.gif');
$image_list = array('good' => 'goodimage.gif', 'bad' => 'bad.gif');


$host = $_SERVER['HTTP_REFERER'];
if(preg_match ($VALID_DOMAIN, $_SERVER['HTTP_REFERER'] )
 
$matches = array();
$pattern = '#(^|[^\"\'=\]])(http|HTTP|ftp)(s|S)?://((([^\s<>\.]+)\.)+[^\s<>\.]+)#';
preg_match ($pattern , $VALID_URL, $matches );
$domain_ary = explode("/", $matches[4]);
$matches = array();
preg_match ($pattern, $host, $matches );
 
$publish_url_ary = explode("/", $matches[4]);
if ($domain_ary[0] == $publish_url_ary[0])
     $name = $image_file_directory . $image_list['good'];
     $name = $image_file_directory . $image_list['good'];
else
else
     $name = $image_file_directory . $image_list['bad'];
     $name = $image_file_directory . $image_list['bad'];


  // Date in the past
  // Date in the past

Latest revision as of 00:46, 19 December 2007

<digg />

Description

This PHP code is supposed to prevent remote linking of images. I'm just slapping it down here for now and may make it actually work later.

Caveats

I am just slapping this code down here so you can pick out the bits you like. I've stripped most of the code that's specific to my (work) environment, but haven't tested it since then.

Source

<source lang="php"> <?php //***********************************************// // // this code is freeware // //***********************************************//

$image_file_directory = $DOCUMENT_ROOT . "images/"; // prefix for image names in $image_list $image_list = array('good' => 'goodimage.gif', 'bad' => 'bad.gif');

if(preg_match ($VALID_DOMAIN, $_SERVER['HTTP_REFERER'] )

   $name = $image_file_directory . $image_list['good'];

else

   $name = $image_file_directory . $image_list['bad'];
// Date in the past

header("Expires: Wed, 19 Dec 2007 07:41:51 GMT"); // always modified header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // HTTP/1.1 header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); // HTTP/1.0 header("Pragma: no-cache"); // send the right headers $pathinfo = pathinfo($name); $subtype = $pathinfo['extension']; header("Content-Type: image/$subtype"); header("Content-Length: " . filesize($name));


// open the file in a binary mode $fp = fopen($name, 'rb'); // stream the picture out to the browser fpassthru($fp); fclose($fp);

?> </source>