Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

image loading in php from remote server

loading images from local server vs remote server

2:50 am on Nov 11, 2013 (gmt 0)

New User

5+ Year Member

joined:Mar 3, 2010
posts: 5
votes: 0


I am using CHImageGuard PHP Script on my site to read images from local server (via path) and serve it through php to webpages, to hide the image url and direct access to images.

To reduce 'load' on my current server, now I have signed up for another host for static hosting, just to use them as image server, so this CHImageGuard Script will now be loading images from new host and displaying in web / php pages in old website.

The question is, will this actually reduce load on my old website?

will those images be served directly from new image server to browsers, or php will fetch / read these images from new server to old server (cache or memory) and then serves to browsers?

The image code is given below,



// ---------------- CONFIGURABLE SECTION -----------------

// Please modify the following or it will not work on
// your website.

// Where did you actually put your images?
// Make sure that the path you put below ends with
// a directory slash ("/"). The script below assumes it.
$imagedir = "/put/your/full/image/directory/path/here/" ;

// What are the websites (hostnames) that can use this
// image?
// If your site can be accessed with or without the
// "www" prefix, make sure you put both here. Do not put
// any trailing slashes ("/") nor any "http://" prefixes.
// Follow the example below.
$validprefixes = array (
) ;

// What is the main page of your website? Visitors will
// be directed here if they type
// "http://www.webmasterworld.com/chimage.php"
// in their browser.
$homepage = "http://www.webmasterworld.com/" ;

// What is your email address?
// If you want to be informed when someone tries to use
// this script to access an image illegitimately, you
// must uncomment (remove the "//" prefix) the following
// line and change it to point to your email address.
//$email = "yourname@example.com" ;

// ------------ END OF CONFIGURABLE SECTION ------------


function isreferrerokay ( $referrer, $validprefixes )
$validreferrer = 0 ;
$authreferrer = current( $validprefixes );
while ($authreferrer) {
if (eregi( "^https?://$authreferrer/", $referrer )) {
$validreferrer = 1 ;
break ;
$authreferrer = next( $validprefixes );
return $validreferrer ;

//----------------------- main program -----------------------

$image = $_GET['image'] ;
$referrer = getenv( "HTTP_REFERER" );

if (isset($_GET['image'])) {

if (empty($referrer) ||
isreferrerokay( $referrer, $validprefixes )) {

$imagepath = $imagedir . $image ;

$imageinfo = getimagesize( $imagepath );
if ($imageinfo[2] == 1) {
$imagetype = "gif" ;
elseif ($imageinfo[2] == 2) {
$imagetype = "jpeg" ;
elseif ($imageinfo[2] == 3) {
$imagetype = "png" ;
else {
header( "HTTP/1.0 404 Not Found" );
exit ;

header( "Content-type: image/$imagetype" );
@readfile( $imagepath );

else {

if (isset($email)) {
mail( $email, "Bandwidth Theft Alert",
"WARNING:\n\n$referrer\ntried to access\n$image\n",
"From: CHImageGuard <$email>" );
header( "HTTP/1.0 404 Not Found" );
else {
header( "Location: $homepage" );

11:36 pm on Nov 11, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:July 19, 2013
votes: 0

The script should go on the site/server you're hosting the images on if you want to "lighten the load" on your current box and you'll have to change the <img src=""> in the HTML of your site to use the new hostname [meaning your new domain or sub-domain depending on how you set things up].

Also, unless both sites are within the same root-directory at the same host the script above won't work if you install it on in current [first] hosting account, because it walks the file-path to get the images, but if you have two different hosting accounts a script can't "walk from one to the other", so my guess is, not only should it go in the new hosting account, it will have to.

BTW: ergei() has been deprecated since PHP 5.3.0 and should be changed to the preg_ equiv. otherwise in some future release of PHP the script will most likely stop working very suddenly leaving your site spewing errors everywhere there should be images.