Streaming Buffer output shows after PHP script finishes execution - php

I have been experimenting with:
ob_implicit_flush(true);
ob_start();
$timelimit=10;
while ($timelimit>=0){
$timelimit=$timelimit-1;
echo "1";
sleep(1);
flush();
ob_flush();
}
ob_end_flush();
php.ini:
output_buffering = Off
I expect to see "1" added every second until the script completes execution. Instead I see everything only after the script has ended.
What am I doing wrong here?

It was working the whole time. I was calling the php via ajax not realizing that the ajax was the cuplrit
if ((this.readyState == 4 || this.readyState == 3) && this.status == 200) {

Related

session_start() takes long time and makes pages pending

I've used the session_start() func in the ajax files.
First File:
<?php
// First File
if(session_id() == '')
session_start();
sleep(10);
And Second File: (run after 10 seconds, I don't know Why!)
<?php
// Second File
if(session_id() == '')
session_start();
echo 'Second File'; // print after 10 seconds...
When I'm run two Ajax files at the same time, the second file will wait(pending) until the first file to be completed.
What is the reason? The problem has resolved when i removed session_start().
How to solve this problem?
Solution:
session_write_close(); and PHP Session Lockes
<?php
// First File
if(session_id() == '')
session_start();
session_write_close();
sleep(10);
Solution: session_write_close(); and PHP Session Lockes
<?php
// First File
if(session_id() == '')
session_start();
session_write_close();
sleep(10);
sleep(10) means wait for 10 seconds.It is used for adding delay in code.
check here

php infinite loop and comet?

Something seems wrong with my php script, but I have no idea what it is. The only possible thing that seems to be wrong is something to do with the cache, but I am not sure. Here's my script, I'll tell you what's happened below the code:
<?php
set_time_limit(0);
header('Content-Type:text/event-stream');
$prevmod=$lastmod=filemtime('chattext.txt');
function waitformod(){
global $lastmod;
global $prevmod;
while($prevmod==$lastmod){
usleep(100000);
clearstatcache();
$lastmod=filemtime('chattext.txt');
}
echo 'data:'.file_get_contents('chattext.txt').PHP_EOL.PHP_EOL;
flush();
$prevmod=$lastmod;
}
while(true){
waitformod();
}
?>
This is supposed to be used with the JavaScript EventSource and send the contents of chattext.txt whenever it is modified. The file does not output anything, however. I think it is because of the infinite loop. Is there any way to fix this?
Does something like this work better?
<?php
set_time_limit(0);
header('Content-Type:text/event-stream');
$prevmod = $lastmod = filemtime('chattext.txt');
function waitformod(){
global $lastmod;
global $prevmod;
while($prevmod == $lastmod) {
usleep(100000);
clearstatcache();
$lastmod = filemtime('chattext.txt');
}
echo 'data:'.file_get_contents('chattext.txt').PHP_EOL.PHP_EOL;
flush();
$prevmod = $lastmod;
}
while(1) {
waitformod();
}
Your current code looks like it reads the file, outputs it, waits for it to change, and then terminates.

How to stop ignore_user_abort once started?

Whats the best solution to stop a ignore_user_abort actively running on the server in a loop? (PHP)
Basically trying to stop a infinitely looping PHP script that won't stop because ignore_user_abort was set and the script executed ..
Simple exaggerated example :
<?
ignore_user_abort(true);
set_time_limit(0);
$test = 1;
while ($test < 1000000000000000 )
{
sleep(1);
$test = $test+1;
}
?>
It seems connection_aborted is what you're looking for?
<?php
ignore_user_abort(TRUE);
set_time_limit(0);
while (TRUE) {
if(connection_aborted()) {
break;
}
sleep(5);
}
// do some stuff now that we know the connection is gone
?>
Alternatively you could check connection_status against the predefined CONNECTION_ABORTED constant:
if(connection_status() == CONNECTION_ABORTED)
{
break;
}
You can get a good rundown in the manual docs on Connection handling
//this will stop the script after running for 30 sec
set_time_limit(30);

Codeigniter 2.x - Echo output as script is working

I was trying to get codeigniter to output text as the script was working but couldn't get it to work. I have search on here and google and seen using ob_end_flush(); and flush(); and also along with adding more bytes so the browser can output. But none of that is working in CI 2.x. If anyone has had luck with this, thanks in advance
I have tried
function test()
{
ob_end_flush();
echo "test1";
ob_start();
sleep(3);
ob_end_flush();
echo "test1";
ob_start();
sleep(3);
ob_end_flush();
echo "test1";
ob_start();
}
With no luck. The script waits 6 seconds then spits everything out at once. I would like it to echo the output to the screen then wait 3 seconds then output the next echo then wait another 3 seconds etc.
I tried this today and didn't worked either. Then I looked at the core's output class and there was a private _display() function. I figured that the output is collected before it's displayed into some variable then at last this function is called. So before my code in the controller method, I added this line:
$this->output->_display("");
and then ran the code. It worked. So your modified function would be like this :
function test()
{
$this->output->_display("");
ob_end_flush();
echo "test1";
ob_start();
sleep(3);
ob_end_flush();
echo "test1";
ob_start();
sleep(3);
ob_end_flush();
echo "test1";
ob_start();
}
The issue you're having with Code Igniter specifically is that there is already an output buffer in effect. Preceding your test with the following snippet will get you out of php-level buffering at least:
// try to bust out of output buffering
while(ob_get_level()) {
ob_end_flush();
}
ob_end_flush();
As noted by #Wesley, this can still be undermined by your server's configuration, but in my current setup I can stream output back after busting out of all output buffers.
check your server api with
echo phpinfo();
if you found your server api
Server API : CGI/FastCGI
in CentOS Add below line in "/etc/httpd/conf.d/fcgid.conf"
OutputBufferSize 0
Restart your Apache server and try below code
ob_start();
for($i = 0; $i < 10; $i ++) {
echo $i;
echo '<br />';
flush();
ob_flush();
sleep(1);
}

Using PHP how to echo content every certain amount of time?

Code:
echo "1";
sleep(1);
echo "2";
sleep(1);
echo "3";
What am trying to do is have the script echo "1" in the screen wait for one second then display "2" etc... As is the script waits for 2 seconds then displays all content at one. All i know about this is that it has do to with buffering
Disable output buffering by flushing at the beginning of script, and activate implicit output buffer flushing. This should do it:
ob_implicit_flush(true);
ob_end_flush();
for ($i=0; $i<5; $i++) {
echo $i.'<br>';
sleep(1);
}
Use ob_start(); to catpure the output in combination with ob_flush(); flush(); to send it out to the browser, periodically.
So your example would become:
ob_start();
echo "1";
ob_flush(); flush();
sleep(1);
echo "2";
ob_flush(); flush();
sleep(1);
// ...
I don't think this is the classy way to do something like this. These kind of stuff needs to be done in client side with javascript not server side with php.

Categories