Solved

make php page load faster

Posted on 2004-10-12
21
211 Views
Last Modified: 2006-11-17
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?
0
Comment
Question by:cimmer
  • 7
  • 4
  • 2
  • +4
21 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 350 total points
ID: 12287705
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
 
LVL 3

Expert Comment

by:alskdj80
ID: 12287708
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12287723
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12287730
to the users browser*
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12292308
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
 
LVL 2

Expert Comment

by:ThePCNerd
ID: 12292594
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
 
LVL 2

Expert Comment

by:JconServ
ID: 12294139
You could also make sure that you have output buffering disabled in your php.ini
0
 
LVL 5

Expert Comment

by:basiclife
ID: 12297564
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
 

Expert Comment

by:Egghead1960
ID: 12299516
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 2

Expert Comment

by:ThePCNerd
ID: 12300046
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
 
LVL 2

Expert Comment

by:JconServ
ID: 12304707
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
 
LVL 1

Expert Comment

by:rweston
ID: 12475291
Sound like cache control on the client side:

try:

header("pragma: no-cache");
header("Cache-Control: private");
header("Cache-Control: no-cache");
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12665062
My recommendation: Accept Diablo84 {http:#12287705}

To output data as it has been processed on the server you must flush the output buffer.
0
 
LVL 2

Expert Comment

by:ThePCNerd
ID: 12826092
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12826308
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
 
LVL 5

Expert Comment

by:basiclife
ID: 12827208
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
 
LVL 2

Expert Comment

by:ThePCNerd
ID: 12834595
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12835413
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now