Link to home
Start Free TrialLog in
Avatar of cimmer
cimmer

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Diablo84
Diablo84

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alskdj80
alskdj80

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
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.
to the users browser*
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.
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)
You could also make sure that you have output buffering disabled in your php.ini
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
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.
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.
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.
Sound like cache control on the client side:

try:

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

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

https://www.experts-exchange.com/questions/21165151/make-php-page-load-faster.html#12292594

Data in tables will not show on many browsers because of the way table widths and heights are created without defining them.
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 :)
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
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.
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