make php page load faster

I have a PHP page that loads at a fairly normal speed. (5 seconds or so). The problem is the page waits to display everything until all the content is loaded. So for about 5-6  seconds the user sees a blank screen and then suddenly everything loads instantly.

How can I get the page to load piece by piece as the content downloads?
cimmerAsked:
Who is Participating?
 
Diablo84Connect With a Mentor Commented:
you can use the flush function but this will only work well on a Unix/Linux based server:

http://www.php.net/manual/en/function.flush.php
0
 
alskdj80Commented:
to my knowledge, you cant do this... ive noticed that IE loads it more piece by piece than firefox/netscape does... this depends on the browser rather than the php code
0
 
Diablo84Commented:
So after any loops or chunks of code add

flush();

and this will effectively "flush" whatever is currently in the output buffer to the users buffer so they will see the content as it is being generated.

As i recall this doesn't work on Windows servers.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Diablo84Commented:
to the users browser*
0
 
Diablo84Commented:
I meant to show you these two examples earlier.

They basically demonstrate how flush works....

<?php
for ($i=1;$i<=10;$i++) {
 sleep(1);
 echo "Loop $i Complete.<br>\n";
 flush();
}
?>

AND

<?php
echo "Loading";
for ($i=0;$i<5;$i++) {
 sleep(1);
 echo ".";
 flush();
}
echo "<br><br>Complete!";
?>

In your case you will probably be wanting to call the flush function at regular points in your code, so if it takes about 5 seconds to load you might add it in 5 places so output is flushed once a second. You can either add it to (for example) every 15th line or code so they are spaced out equally or, the more sensible idea, would be to add it to more intensive code that takes longer to process, loops, function calls anything like that.
0
 
ThePCNerdCommented:
Any tags like <table> if not closed sometimes wait until the end tag is found, this happens for a few different tags as well.

That is part of the problem.

I would print at the top of the page in no tag at all "Please Wait..."

Then print a few backslash characters (I dont know if that works in php but you get the idea)
0
 
JconServCommented:
You could also make sure that you have output buffering disabled in your php.ini
0
 
basiclifeCommented:
FYI: flush() works on PHP/Apache/Windows but not really on PHP/IIS and as Diable says, even though the browser has the data it doesn't mean it will display it until it wants to (ie has it all) - Browser dependant
0
 
Egghead1960Commented:
If the layout of your page uses tables for positioning control, especially multiple levels of nested tables, you will experience signficant loading delays, since tables must load completely before being displayed. If this is the case, you might consider using CSS for layout control, and only using tables where needed for display of tabular data. This will signficantly increase the speed of any page that was formerly laid out using tables.
0
 
ThePCNerdCommented:
Yea, i said that earlier, I have a website that looks like you are on 56k becuase i used tripple tested tables to make it easier on the code i would of had to make.
0
 
JconServCommented:
Just a suggestion in the future, much more can be accomplished with a css based layout. Also it is compatible with most browsers and will make for clean, fast loading code.
0
 
rwestonCommented:
Sound like cache control on the client side:

try:

header("pragma: no-cache");
header("Cache-Control: private");
header("Cache-Control: no-cache");
0
 
Diablo84Commented:
My recommendation: Accept Diablo84 {http:#12287705}

To output data as it has been processed on the server you must flush the output buffer.
0
 
ThePCNerdCommented:
I beleive I hold part of the answer as well.

http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21165151.html#12292594

Data in tables will not show on many browsers because of the way table widths and heights are created without defining them.
0
 
Diablo84Commented:
Possibly, although there is little indication as to whether tables are being used or not. I was thinking along the lines of a processing script as it was asked in the PHP TA (ie. outputting content as data is being looped) although tables can be an issue, especially if they are being used incorrectly for layout.

A reappearance from cimmer would be useful so we at least know what s/he had in mind :)
0
 
basiclifeCommented:
Personally I go with Diablo on this one as you can't "unsend" the data as ThePCNerd suggested (I'm guessing he meant backspace not backslash). Sorry dude
0
 
ThePCNerdCommented:
That was not the answer or my solution that was just a side suggestion. The point was if you use tables it wont show most data until the browser decides to update the output or the end tag is found; which if he did not know would have fixed his problem if he switched to css or another method to align data.

Regarding the hiding the loading idea...
You could have javascript change the style of the div to be off the page, or for a non-js enabled browser you could have two divs enclosing the other. The outer tag would be defined by a style placed at the end of the page that hides the div, the inner would be the text's style.

I dont know if browsers will comply to styles not defined then added after the style was used, but that may work. My point is there is a way to make it say loading and hide when loaded other than what I said that you put down.
0
 
Diablo84Commented:
hmm, well i am standing by my original recommendation.

As said before, while it is a possibility, there is no mention of tables being used, we are simply addressing making a "php page load faster". The emphasis is very much on PHP in this question therefore my thinking is still focused around dealing with the processing of a script, loops, conditionals etc. so flush is the most suitable function. I would have thought it would have been asked in the HTML TA if it was loading time with regards to HTML elements. I may well be wrong but unless cimmer makes an appearence to say one way or the other i'm sticking with what i thought in the first place :)

Regards
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.