Page 1 of 1

Server slowing, trying to figure out why - php includes

Posted: Tue Jun 21, 2011 8:14 pm
by stevebeans
I have a blog network that is pretty connected, and possibly to a fault. Every time my traffic spikes to even mediocre numbers (50+ visitors at a time or so), my cpu spikes and the network crawls. I am checking out server issues, hardware, etc, but I am also looking to make sure my code is clean. I'm a huge php newbie.

My blogs are cached, but I noticed when I loaded it, my sidebar was a bit delayed, which made me wonder if php was an issue. Right now my sidebar is pretty "include-heavy", as I have quite a few php files I'll call cross server so I can quickly alter banners and what not.

Right now I'm using file_get_contents, and doing it like this (which I already realized is wrong)

Code: Select all

<?
$c = file_get_contents("http://www.url.net/includes/file1.php");
echo ($c);
?>	
<?
$c = file_get_contents("http://www.url.net/includes/file2.php");
echo ($c);
?>
I already see one problem that I'm opening and closing php like crazy, so I'll correct that. Another problem is I appear to be using the same variable.... now that I am learning php a bit, I would like to put this in the header:

Code: Select all

$url1 = file_get_contents("http://www.url.net/includes/file1.php");
$url2 = file_get_contents("http://www.url.net/includes/file2.php");
Then on the sidebar call

Code: Select all

<?php echo $surl1 ?>
blah blah blogroll
<?php echo $url2 ?>
I'm still not sure that would even help the situation at all, isn't there an easier include feature that doesn't put all the data into a variable before it's called? To me that seems to be the biggest strain right there.

Sorry if this is a dumb question.

Re: Server slowing, trying to figure out why - php includes

Posted: Wed Jun 22, 2011 8:45 am
by Eric!
I have quite a few php files I'll call cross server
It would be a lot faster if you kept all your executable code local to the server. So you could write some code that made a local cache of your executable php code and then use a standard include("filename.php"); call. Executing remote code is not the most secure way to run a site.

Re: Server slowing, trying to figure out why - php includes

Posted: Wed Jun 22, 2011 10:24 am
by AbraCadaver
What's wrong with just:

Code: Select all

include('includes/file1.php');

Re: Server slowing, trying to figure out why - php includes

Posted: Wed Jun 22, 2011 10:43 am
by stevebeans
AbraCadaver wrote:What's wrong with just:

Code: Select all

include('includes/file1.php');
Sometimes it'll be cross domain, and cross account in my VPS.

ie, an example of what I'm doing would be the top bar here:

http://www.teenmomjunkies.com/

the "junky network". I'd like to have that bar on all 3 of the pages calling a single file. This is a very small example and obviously it's just 3 pages and easy updates, but I do have some files in my network that are called 50+ times via includes, and that becomes tedious to update.

Re: Server slowing, trying to figure out why - php includes

Posted: Wed Jun 22, 2011 10:49 am
by stevebeans
Eric! wrote:
I have quite a few php files I'll call cross server
It would be a lot faster if you kept all your executable code local to the server. So you could write some code that made a local cache of your executable php code and then use a standard include("filename.php"); call. Executing remote code is not the most secure way to run a site.

So what you're saying is have my file at say:

http://www.url.com/includes/file1.php

then on my www.secondurl.com I take file1.php from url.com, make a cache of it on secondurl.com and just do the easy include("cachedfile.php")?

I guess the problem there is I have no clue how to turn file1.php into a cached file on my secondurl :(

Re: Server slowing, trying to figure out why - php includes

Posted: Thu Jun 23, 2011 2:48 pm
by Eric!
Just make a local copy so you aren't running remote code all the time, because it's slow and insecure.

Something basic would look like this:

Code: Select all

//is a local copy available in the cached directory?
$file_url1="http://remote.site1.com/path/to/file/";
$file1="file_to_include.php";
$cache_dir="cache/";
if(!file_exits($cache_dir . $file1) { 
  $cache=file_get_contents($file_url1 . $file1); // retrieve content
  file_put_contents($cache_dir . $file1,$cache); // make a local copy
  include($cache_dir . $file1);  // run it
}
else include($cache_dir . $file1);
You can make this more sophisticated to check to make sure the file dates and sizes match too using filesize() and filetime() instead of just checking if file_exists() or not. This will help keep your cached files updated automatically.

As an FYI this is not a very good practice. You would be better off making a script that updates all local copies on each server and make all your php run on the local server only. You could just make a /SHARED/ directory that would contain common code used on all the servers. Then if the source for that code is changed, a script can be ran to update all the servers with the new change. This would keep each server in sync and allows you to force changes on one server to propagate to the others.

Re: Server slowing, trying to figure out why - php includes

Posted: Thu Jun 23, 2011 5:54 pm
by pickle
Neither of the "problems" you quoted are causing your issue. The effect of the first is so small as to be insignificant. WordPress templates have that stuff all over the place.

The reason people keep asking about the remote loading is because that is what's causing the slowdown. You need to find someway to not load files cross-domain before the page is sent to the browser.