Continuous or Instant HTTP Response?

Hey Sitepoint,

I was hoping I could get a long looming question answered. I would like to know why some Webservers will display a page request all-at-once and sometimes the page comes in piece-by-piece.

Let me try and state that in another way if you don’t quite get what I’m saying. I have a script that I’ve written in PHP and it can take anywhere from 30-60 seconds to run. When the script runs from a local LAMP box I have on my LAN the script reports nothing back to the browser until the very end of the scripts execution, then it displays all at once instantly. When I run this script from my public hosted VPS it loads the page as it appears to be executing the loops and commands in the script.

I’m sure it’s some kind of configuration but I don’t even know what this is actually called.

Any ideas or am I sounding silly?

You can change the value of output_buffering in php.ini (or in an .htaccess file using php_value output_buffering XXXX where XXXX is the value you want to set it to).

The reason it exists is that it is said that streaming everything to a browser directly puts more strain on the server and the browser than browsing it in chunks (mostly because with chunks you can actually fill network packets, instead of putting just a few bytes in them and then send them off half empty).
It also helps prevent the “cannot set cookie because headers already sent”, but that’s more fighting the symptoms than the cause (which is poor programming style).

So you saying it’s harder on the Browser and Server to stream data before the script has fully completed versus sending it all at once when the script is finished?

Well, not exactly. The thing is (and this will get technical) that data that is sent over the internet (or any other typical TCP/IP network) will be sent in so called “packages” of 1500 bytes. Every package has to be 1500 bytes, no smaller, no bigger. That’s just how it is. So suppose I have 100 bytes ready and send it to you, I’m wasting 1400 bytes (padding zeroes) I could have also used to send you data. Do this often enough and streaming like this will cost a lot of overhead and time for nothing.

Compare this with a toy manufacturer that only has boxes of 20 square foot. What would be the better choice, send one marble per box, or wait until there are more marbles, stuff a whole lot of them in the box, and then send it? :slight_smile:

That makes sense. How would I adjust that for future reference and what is the point of the feature existing?

'Sounds like you have PHP’s output_buffering set to “On” on one server, and off (or some low enough value) on the other :slight_smile:

The one with output buffering set to On will first save all output to a memory buffer and then send everything to the browser when it’s done, while the other one will just send data to your browser when it gets it.