note 91556 added to function.flush
| From: | jason at jasonbaumgartner at info | Date: | Wed, 17 Jun 2009 00:18:18 +0000 |
| Subject: | note 91556 added to function.flush | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156947@lists.php.net to get a copy of this message | ||
Many of the suggestions below are very helpful but I would just like to clarify something.
If you are making calls to a .php script with AJAX (xmlHttpReq) and use flush, this will send data
to your script HOWEVER it will not set the xmlHttpReq.readyState to 4 -- which is a requirement to
use the information sent (Firefox does however allow you to use the responseText property with a
readyState == 3 but IE will throw an error).
To get around this, you will need to make sure of a couple things:
In your Apache php.ini config file, check to make sure that output buffering is disabled:
output_buffering = off
Next, disable gzip compression for the .php script that is called from your AJAX script by using the
excellent suggestion from Mandor by placing the following at the top of your script:
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
What causes this problem is that while the php child process is running under apache, apache is
waiting for the script to complete before completely closing the connection. Scripts that send
information directly to the browser with echos and prints will get away with using flush followed by
a sleep command and then clean up procedures. However, if you notice in the status bar of your
browser window, the connection is still held open by apache until the script completed
("Transferring" ... yadda yadda).
By turning off the Apache compression, the connection is terminated directly after a flush while
still giving the user the ability to run a sleep command followed by clean up code.
This bit me because the AJAX was returning <img> tags to newly created images by php. The
cleanup script would then remove those images after completion.
Since Apache held the connection open, the readyState only changed to 4 AFTER my php cleanup fired
-- thereby erasing the links referred to my the <img> tags.
With this solution, the readyState will change to 4 BEFORE the sleep command begins.
I hope this saves someone a lot of time and frustration. AJAX requests are an entirely different
beast compared with simple outputting to a browser window.
----
Server IP: 69.147.83.197
Probable Submitter: 75.70.230.222
----
Manual Page -- http://www.php.net/manual/en/function.flush.php
Edit -- https://master.php.net/note/edit/91556
Del: integrated -- https://master.php.net/note/delete/91556/integrated
Del: useless -- https://master.php.net/note/delete/91556/useless
Del: bad code -- https://master.php.net/note/delete/91556/bad+code
Del: spam -- https://master.php.net/note/delete/91556/spam
Del: non-english -- https://master.php.net/note/delete/91556/non-english
Del: in docs -- https://master.php.net/note/delete/91556/in+docs
Del: other reasons-- https://master.php.net/note/delete/91556
Reject -- https://master.php.net/note/reject/91556
Search -- https://master.php.net/manage/user-notes.php