Server slowing, trying to figure out why - php includes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stevebeans
Forum Newbie
Posts: 6
Joined: Sat Feb 05, 2011 11:38 am

Server slowing, trying to figure out why - php includes

Post 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.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

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

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

What's wrong with just:

Code: Select all

include('includes/file1.php');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
stevebeans
Forum Newbie
Posts: 6
Joined: Sat Feb 05, 2011 11:38 am

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

Post 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.
stevebeans
Forum Newbie
Posts: 6
Joined: Sat Feb 05, 2011 11:38 am

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

Post 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 :(
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

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

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply