Click to See Complete Forum and Search --> : PHP Header for Timeout?
shane.carr
06-18-2008, 08:37 PM
I am programming an AJAX web application, but I am encountering a potential problem regarding timeouts. Some browsers (notably Safari) will "wait" only 1 minute for a request to be sent back from PHP. Here is a bit of my code:
$initquery = "select * from MyTable..."; // a MySQL query
do{
sleep(1);
$result = mysql_query($initquery);
$result = mysql_fetch_assoc($result);
}while($result["continue"]=="no");
The code works fine. The problem is, if $result["continue"] does not change to "yes" within a minute (this is done by other scripts), the browser will stop waiting and issue a timeout error. Is there a way to make the browser wait longer for a response? I was thinking that this could be done with some kind of header that sets timeout at five minutes or something (like header('Cache-Control: no-cache, must-revalidate'); makes the browser not cache the page). Is this possible? Thanks in advance.
.
NogDog
06-18-2008, 08:49 PM
I do not know if there is some way to address it in that manner.
I'd probably consider putting the while loop into the client side JavaScript, and simply have the server side PHP script return the continue no/yes response as part of the result data so that the JavaScript can then decide whether or not to request it again or process the result.
shane.carr
06-18-2008, 09:47 PM
I thought about that, but it is easier on the client-side to have to send only 1 request. I have another script elsewhere that sends a request similar to this one every 5 seconds, which works sort-of well, with the exception of the occasional "stall", in which scenario I have to abort the request and start a new one.
I will probably end up doing something similar to that, but, rather than sending a new request every few seconds, it will "refresh" the current one every 50 seconds.
If you (or anyone else) every discovers a convenient header that will fix the timeout issue, I'd be glad to know.
NogDog
06-18-2008, 10:16 PM
Just a thought: what would happen if you output a space or newline every few seconds within the loop, possibly followed by a flush() to encourage the web-server to send it immediately rather than buffering it?
If your making a request for data that is taking more than a minute in response time, ho much data are you requesting in the first place?
AJAX is bes used for small file requests like text files.
My suggestion is to break your data down into manageable chunks that take seconds to deliver rather than minutes. With that type of interface, how do you ever hope to retain visitors? Waiting around for 5 minutes for a web page is not my idea of phun.
NogDog
06-19-2008, 08:16 AM
If your making a request for data that is taking more than a minute in response time, ho much data are you requesting in the first place?
AJAX is bes used for small file requests like text files.
My suggestion is to break your data down into manageable chunks that take seconds to deliver rather than minutes. With that type of interface, how do you ever hope to retain visitors? Waiting around for 5 minutes for a web page is not my idea of phun.
I don't think the problem was the amount of data, but rather the loop that waits until something else (apparently) changes something in the database to the desired value.
shane.carr
07-15-2008, 09:01 PM
I don't think the problem was the amount of data, but rather the loop that waits until something else (apparently) changes something in the database to the desired value.
That is correct.
As I was working on a different project, I stumbled upon this page:
http://www.hpl.hp.com/personal/ange/archives/archives-95/http-wg-archive/1661.html
If I interpreted it correctly, if you send the headers
header("Connection: Keep-Alive");
header("Keep-Alive: timeout=300");
it should set the timeout to 5 minutes. I have not tested it yet.
I ended up writing a script (on the javascript side) that checks with PHP every few seconds. If $result["continue"]=="no", it echos a message to tell javascript to check again in a few more seconds. This method works decently, with the only problem being the occasional "stall" in which php takes as much as 15 seconds to send an answer. But this problem is not very common, and it fixes itself after 15 seconds :rolleyes:
What do you think about the "Keep-Alive" header? Have you ever encountered it before?
.