Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-30-2011, 11:08 PM   PM User | #1
Coastal Web
Regular Coder

 
Coastal Web's Avatar
 
Join Date: Oct 2004
Posts: 225
Thanks: 12
Thanked 3 Times in 3 Posts
Coastal Web is an unknown quantity at this point
flush() - why the heck isn't this working?

Hey guys, any idea why this isn't working?
The output just dumps all to the browser, instead of 1 second increments as l would expect.

PHP Code:
<?

ob_end_flush
();  
flush();

for(
$i=0;$i<5;$i++){
    echo 
'test ' $i '<br />';
    
flush();
    
sleep(1);
}

?>
Coastal Web is offline   Reply With Quote
Old 09-30-2011, 11:11 PM   PM User | #2
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
There's a lot of buffering going on (gz handler, apache etc). Basically, this isn't going to ever work unless you have a very specific and unusual setup (and one that's far from optimal).
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Old 09-30-2011, 11:31 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,513
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Then you get ISP caching...

Plus the socket buffer sizes on your computers OS.. there are so many different buffers between your browser and the script on the server that you'll never achieve the desired result. The only other thing you can do is to repeatedly call the script time and time again Not ideal but it would have the desired effect.. ish.

Basically, unless its a data stream, you can't use php for a live output. Thats because most socket buffers have a size of around 4Kb give or take a few.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 10-03-2011, 03:41 PM   PM User | #4
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
remove / comment out

ob_start();
ob_end_flush();

etc if they appear anywhere

add this to a functions file or somewhere it can be referenced

PHP Code:
/** 
* Output buffer flusher
* Forces a flush of the output buffer to screen useful for displaying long loading lists eg: bulk emailers on screen 
* Stops the end user seeing loads of just plain old white and thinking the browser has crashed on long loading pages.
*/
function fcflush()
{
    static 
$output_handler null;
    if (
$output_handler === null) {
        
$output_handler = @ini_get('output_handler');
    }
    if (
$output_handler == 'ob_gzhandler') {
        
// forcing a flush with this is very bad
        
return;
    }
    
flush();
    if (
function_exists('ob_flush') AND function_exists('ob_get_length') AND ob_get_length() !== false) {
        @
ob_flush();
    } else if (
function_exists('ob_end_flush') AND function_exists('ob_start') AND function_exists('ob_get_length') AND ob_get_length() !== FALSE) {
        @
ob_end_flush();
        @
ob_start();
    }

call your code

and replace flush() with fcflush();
PHP Code:
<?php

for($i=0;$i<5;$i++){
    echo 
'test ' $i '<br />';
    
fcflush();
    
sleep(1);
}

?>
and for gods sake use proper php openings not short openings.
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Old 10-03-2011, 04:39 PM   PM User | #5
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,513
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by hinch View Post
add this to a functions file or somewhere it can be referenced

PHP Code:
/** 
* Output buffer flusher
* Forces a flush of the output buffer to screen useful for displaying long loading lists eg: bulk emailers on screen 
* Stops the end user seeing loads of just plain old white and thinking the browser has crashed on long loading pages.
*/
function fcflush()
{
    static 
$output_handler null;
    if (
$output_handler === null) {
        
$output_handler = @ini_get('output_handler');
    }
    if (
$output_handler == 'ob_gzhandler') {
        
// forcing a flush with this is very bad
        
return;
    }
    
flush();
    if (
function_exists('ob_flush') AND function_exists('ob_get_length') AND ob_get_length() !== false) {
        @
ob_flush();
    } else if (
function_exists('ob_end_flush') AND function_exists('ob_start') AND function_exists('ob_get_length') AND ob_get_length() !== FALSE) {
        @
ob_end_flush();
        @
ob_start();
    }

Not that socket buffer sizes on a windows machine or an ISP can be overridden by PHP but you're still using flush() in your function - which as demonstrated already doesn't work.

Secondly I've just tried using it on my wamp setup and again, it doesn't work through a browser. I did however get it to work up to the 4th loop using a http debugging program.. which then froze. I tried another which did work upto and past 30 however no normal browser will work like this.

Finally if it doesn't work on localhost it certainly won't on a remote server with an ISP between it and the end user due to cache issues and socket buffers.

To put it mildly, your code is unstable.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 10-03-2011, 05:12 PM   PM User | #6
chris0
New Coder

 
Join Date: Sep 2011
Posts: 45
Thanks: 1
Thanked 6 Times in 6 Posts
chris0 is an unknown quantity at this point
as far as i know, the web browser do not display any part of the page untill it received the full file.

and the load images and js files
chris0 is offline   Reply With Quote
Old 10-04-2011, 03:49 PM   PM User | #7
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
Quote:
Originally Posted by tangoforce View Post
To put it mildly, your code is unstable.
The code is very very stable and used in several very large scale commercial projects where its tried and tested. (This very forum uses exactly the same code snippet!)

Not only does it work perfectly on wamp servers but it also works perfectly on unix servers.

If a page flush isn't happening then there's some other issues at work which aren't browser/code related.

In usage you'd use it something like this.

PHP Code:
while (($data fgetcsv($handle3000",")) !== FALSE) {
set_time_limit(60); //reset time limit to stop script timing out
echo "<span style=\"color:green\">$row - Importing Ticket ".$data[4]."</span> - ";
$row++;
//check if ticket already exists
$sql "";
$result mysql_query($sql) or die(mysql_error());
                    if (
mysql_num_rows($result)>0) {
                        echo 
"Ticket Exists - Updating<br />";
                        
$importsql "";
                        
mysql_query($importsql) or die(mysql_error());
                        
fcflush();
                    } else {
                        
$importsql "";
                        
mysql_query($importsql) or die(mysql_error());
                        echo 
"New Ticket - Inserting<br />";
                        
fcflush();
                    }
                } 
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Old 10-04-2011, 03:52 PM   PM User | #8
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,513
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
hinch, I'd invite you to demonstrate it in use but to be honest, I've never once seen any flush call work successfully in a browser.

As I said, for some http based programs such as the one I used yesterday it may work but for webpages it does not.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 10-04-2011, 03:58 PM   PM User | #9
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
i shall send you a PM to a small video
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Reply

Bookmarks

Tags
buffer, flush, output

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:57 PM.


Advertisement
Log in to turn off these ads.