Why doesn't the php command refresh work in Safari?
P粉141925181
P粉141925181 2023-09-04 18:39:18
0
2
651
<p>I have a time consuming php script so I wanted to give the users some feedback. Simplify it to solve the core problem. The script runs perfectly fine in Firefox (refreshes the output immediately), but Safari waits for all content to be generated. Why? How can I solve this problem? </p> <pre class="brush:php;toolbar:false;"><?php ini_set('max_execution_time', 0); header('Content-Encoding: none;'); header('Content-type: text/html; charset=utf-8'); $j = 8; $k = pow(2, 10); echo "One moment please...".str_pad('', $k)."<br />\n<br />\n"; flush(); $i = 0; while ($i < $j) { $i; echo "Test ".$i.str_pad('',$k)."<br />\n"; flush(); sleep(1); } ?></pre> <p>BTW: Chrome won't load this page at all, it will give me <code>ERR_CONTENT_DECODING_FAILED</code>. </p> <p>Also I tried to put</p> <pre class="brush:php;toolbar:false;"><IfModule mod_env.c> SetEnv no-gzip 1 </IfModule></pre> <p> in .htaccess but no luck. Also tried <code>SetEnv no-gzip dont-vary</code>. Any clues on how I can get Safari (and Chrome) to do the same thing as Firefox? </p> <hr /> <p>I read on php.net about <code>flush()</code>, <code>ob_flush()</code>, <code>ob_start()</code> ; etc., and I've read almost every comment on Stackoverflow on every question about flush over the past 20+ years. I tried adding <code>ini_set('output_buffering', 'On');</code> or <code>ini_set('output_buffering', 'Off');</code>, adding <code> ob_start(null,Chapter 4096<code>header('X-Content-Type-Options: nosniff');</code> and/or<code>header('X-Accel-Buffering: no') ;</code>. I put <code>@ob_flush();</code> before or after <code>flush();</code> and tried putting <code>str_pad< Raising the value in /code> to 2^16 (65,536) did not help.</p><p> Unfortunately, I can't seem to control the Apache server (running PHP 8.1 and FastCGI) at my hosting provider. Now that it works on Firefox, I guess I must be doing something right. </p>
P粉141925181
P粉141925181

reply all(2)
P粉684720851

Thankshareth pyI found the answer!

<?php 
ini_set('max_execution_time', 0);
header('Content-Encoding: none;');
header('Content-type: text/html; charset=utf-8;');
ob_start("ob_gzhandler");
$j = 200;
$k = pow(2, 10);

echo "One moment please...".str_pad('', $k)."<br />\n<br />\n";


$i = 0;
while ($i < $j) {
    $i++;
    echo "Test ".$i.str_pad('',$k)."<br />\n";
    ob_flush();
    usleep(100000);
} 
?>

I increased $j to 200 and lowered the sleep time to 0.1 seconds. Safari does respond a little slowly, but eventually it displays line by line. Just like Firefox and Chrome.

P粉762447363

edit:
ob_start("ob_gzhandler") Intended to be used as a callback function for ob_start() to help send gz-encoded data to web browsers that support compressed web pages. source

<?php
ini_set('max_execution_time', 0);
header('Content-Encoding: none;');
header('Content-type: text/html; charset=utf-8');
ob_start("ob_gzhandler");
$j = 8;
$k = pow(2, 10);

echo "One moment please...".str_pad('', $k)."<br />\n<br />\n";


$i = 0;
while ($i < $j) {
    $i++;
    echo "Test ".$i.str_pad('',$k)."<br />\n";
    ob_flush();
    sleep(1);
}
?>

Output (on Chrome):

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template