homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

image loading in php from remote server
loading images from local server vs remote server

5+ Year Member

Msg#: 4622416 posted 2:50 am on Nov 11, 2013 (gmt 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" );




WebmasterWorld Senior Member Top Contributors Of The Month

Msg#: 4622416 posted 11:36 pm on Nov 11, 2013 (gmt 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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved