John
John

Reputation:

PHP flush function causing weird characters on the screen

After so much trouble I find out that when I use the flush function in my PHP mail script then I get garbage or dump characters on browser like below.

alt text

The code is below

if ($mail->Send()) {
    echo "<br><font color=darkgreen>[$num successful send to $to]</font> ";
    // flush();
    return true;
}

If I comment that flush line then out is simple English but I uncomment that the whole page the text looks like garbage.

Now is that a PHP problem, browser problem or server problem?

If I use the same script from the shell, I mean execute inside the shell terminal then I can see the HTML output. But it does not work in browsers.

Upvotes: 1

Views: 241

Answers (2)

Rory Abraham
Rory Abraham

Reputation: 116

What fixed this for me was to use ob_start before my echo statements, then use ob_end_flush and flush after them:

for ($i = 0; $i < 6; $i++) {
    ob_start();
    echo "$i";
    ob_end_flush();
    flush();
    sleep(1);
}

Upvotes: 0

John
John

Reputation:

I found the answer to my own question. I had to turn

zlib_compression off

in my php.ini settings file.

(What does that mean and why did it work?. I had been trying this for 1 year but could not resolve the problem but now it worked.)

Upvotes: 0

Related Questions