Forum Moderators: coopster

Message Too Old, No Replies

ob start

         

Dunjohn19

1:58 pm on Aug 15, 2020 (gmt 0)

5+ Year Member



Hello everyone,

I have placed images outside of the root directory and they are successfully loaded on my pages. I have topics organized by icons, which are 200px png images or jepeg images. I use an array to store the topic names. I loop through the array to dynamically create the pages topics and load their icons. I noticed that the icons sometimes drop into the page after it is loaded. I do not like this dropping appearance. I was trying to think of ways to load them all at once with the rest of the page.

I remember ob start buffers. I added ob_start and ob_end_flush before and after the for each loop that displays the dynamic content. The code is working and the icons to do not just drop onto the page anymore. However, i wonder if this is an appropriate use of ob start? is this okay to implement ob start? will it cause any problems with apache or php in a live website?

Thank you for any replies. Best wishes, John

lucy24

5:07 pm on Aug 15, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think the canonical use for ob is when you need to start assembling something, but you don’t yet know if it will end up being pushed out as a user-visible page. At least, that’s the main thing I have used ob for in the past. (I also use it in header logging as it seems to cut down on chaos when two requests come in right on top of each other, though I can’t swear that the effect follows from the cause.) But yours seems to be a close second: when it will take time to build the page, and you don’t want anything to be sent to the user until it’s ready to go.

Horse’s mouth [php.net] says:
Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.
This, frankly, fills me with terror.

What I was actually looking for was a pronouncement on when ob is appropriate [php.net], but they’re not awfully helpful:
especially if you need to send headers to the browser after your script has begun outputting data
“No, wait, cancel that, I couldn’t find the database so we’ll need to send out a 404 instead” which can only be done if you haven’t sent out any content yet.

Edit: What’s wrong, though, with images showing up after the page has started loading? It may be preferable to making the user wait for several seconds while everything is assembled in the background. It’s only a problem if the images don’t include width and height declarations, so the page might have to be redrawn by the browser as it gets more information.

Dunjohn19

8:23 pm on Aug 15, 2020 (gmt 0)

5+ Year Member



Hi Lucy24,

Thank you for replying. I appreciate you very much.

I can't find much of a usage guide at php.net other than what you mentioned in your reply. I suppose that my usage of ob start is not appropriate. I've removed the code to keep on the safe side of things. I don't notice the odd loading/dropping effect on every page. Once every few pages i notice this odd loading/rendering effect. It seems harsh at times so i was trying to make it smoother. I will just have to move on and accept this odd loading.

i have inline width and height attributes, then i use css for responsiveness, id est, width: 100%; height: auto. I load the images using $_GET['image'] (after security filtering etc). I then send the image headers before a readfile output. I think that this odd dropping effect only happens when the page has over 30 icons/topics.

Best wishes, John.

JorgeV

6:06 pm on Aug 16, 2020 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Hello,

ob_start, can also be used to control the buffering of output. And decide what to send, when.

Jonesy

4:01 pm on Aug 22, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



In the old, slow days, I used ob_start and ob_flush to get the first "screenfull"
out to the visitor. Now-a-days that isn't so necessary. Not too many folk on
1200 baud dial-up modems anymore.

Too, about 10 years ago my web host moved to caching servers and that
negated the function of ob_start ... ob_flush.

JorgeV

2:45 pm on Aug 24, 2020 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Hello,

In case of dynamic page (no cache), if you know, that a part of the page, will require "some time" to be generated, it can still make sense to send to the browser all what was already echoed, before this lengthily operation.

lucy24

3:18 pm on Aug 24, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thinking it over, the line from php docs about headers does make sense. It is often simplest and most logical to build a page from top to bottom: html head, boilerplate, variable part of content, more boilerplate. But that means you have to start assembling content before you know if all the content will be available. The output buffer allows you to throw away the first part of the work and proceed to Option B.