homepage Welcome to WebmasterWorld Guest from 54.161.247.22
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
kitaabghar




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

hi,

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,

Hassan


<?php

// ---------------- 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 (
"webmasterworld.com",
"www.webmasterworld.com"
) ;

// 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 ------------


// --- YOU NEED NOT MODIFY ANYTHING AFTER THIS LINE ---

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" );
}

?>

 

JD_Toims




msg:4622646
 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