/    Sign up×
Community /Pin to ProfileBookmark

PHP Header for Timeout?

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:

[code=php]$initquery = “select * from MyTable…”; // a MySQL query
do{
sleep(1);
$result = mysql_query($initquery);
$result = mysql_fetch_assoc($result);
}while($result[“continue”]==”no”);[/code]

The code works fine. The problem is, if [FONT=”Courier New”]$result[“continue”][/FONT] 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 [FONT=”Courier New”]header(‘Cache-Control: no-cache, must-revalidate’);[/FONT] makes the browser not cache the page). Is this possible? Thanks in advance.

[COLOR=”White”].[/COLOR]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 19.2008 — 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.
Copy linkTweet thisAlerts:
@shane_carrauthorJun 19.2008 — 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 [i]sort-of[/i] 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.
Copy linkTweet thisAlerts:
@NogDogJun 19.2008 — 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?
Copy linkTweet thisAlerts:
@rootJun 19.2008 — 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.
Copy linkTweet thisAlerts:
@NogDogJun 19.2008 — 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.[/QUOTE]

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.
Copy linkTweet thisAlerts:
@shane_carrauthorJul 16.2008 — 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.[/QUOTE]

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
[code=php]header("Connection: Keep-Alive");
header("Keep-Alive: timeout=300");[/code]

it [i]should[/i] 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 [FONT="Courier New"]$result["continue"]=="no"[/FONT], it echos a message to tell javascript to check again in a few more seconds. This method works [i]decently[/i], 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?


[COLOR="White"].[/COLOR]
×

Success!

Help @shane_carr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...