LemonInflux Posted June 6, 2008 Share Posted June 6, 2008 Hello. I'm doing a project where I need PHP data to be outputted immediately. So for something like this (an example I took from php.net for the sake of demonstration): <?php echo date('h:i:s') . "\n"; sleep(10); echo date('h:i:s') . "\n"; ?> Normally, this code outputs the 2 after 10 seconds. What I want is for the first one to be outputted instantly, then after 10 seconds the next one is. How is this done? Quote Link to comment Share on other sites More sharing options...
LooieENG Posted June 6, 2008 Share Posted June 6, 2008 Edit: Wait, I don't understand the question Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 6, 2008 Share Posted June 6, 2008 Web servers are designed to output a requested web page and browsers are designed to render that web page. Due to several steps of buffering and compression that php, web servers, and browsers can do, you should not waste your time trying to get a single requested web page to operate this way. You can use multiple http requests to operate this way, using AJAX. The main page is requested and displayed normally, then AJAX is used to make additional http requests to the web server to get and update the information on the page being displayed in the browser. If you describe what you are actually trying to do, someone can provide specific direction on how to do it. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 6, 2008 Share Posted June 6, 2008 I understand your question, but can't think of any solution (other than using AJAX). I guess PHP is just build that way - the whole script is parsed, and then any output is outputted at once. Same reason an error-free echo statement at the start of the script will not output if an fatal error is found later in the script. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted June 6, 2008 Author Share Posted June 6, 2008 ah ok. What I'm trying to do is build an installer that has 'live' updates on progress. So, for example, it would have: <?php // Create a file code here... echo "file has been created!"; // Write to file code here... echo "file has been written to!"; ?> That sort of thing, so the user can see how the install is doing as it happens. How would this be done with AJAX? Quote Link to comment Share on other sites More sharing options...
ingeva Posted June 6, 2008 Share Posted June 6, 2008 I am no expert on PHP but I guess this has to do with output buffering. PHP accumulates all output and releases it when the buffer is either full or flushed, or the script is finished. I suggest you read a little about output buffering in PHP. I don't know enough about it to explain any more, but I'm confident that this will solve your problem. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 6, 2008 Share Posted June 6, 2008 I tested the buffer method, but it doesn't work that way. Found this script at http://www.phpit.net/article/output-buffer-fun-php/: <?php // Enable output buffering ob_start(); // Print some output echo 'Hello World'; // Flush output buffer (thus sends 'Hello World' to the client) ob_flush(); // Print some more output echo 'Hi There'; ?> and tried to put in sleep(10) after the ob_flush(). Still it was all outputted after 10 seconds. AJAX is not something you just copy/paste, so search for some tutorials and learn through experience. What you're looking to achieve can surely be done with AJAX. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 6, 2008 Share Posted June 6, 2008 Yes, you can do it with flush(). If you read the notes on the manual page, you will see why it doesn't work with such short output. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 6, 2008 Share Posted June 6, 2008 Ah okay. Didn't test it in IE though, if you're referring to that part of the note. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.