怎么能避免浏览器请求超时_如何让浏览器超时(长PHP脚本)

The PHP script takes 25 seconds to complete (for 1 person) and probably longer with high traffic. I don't want the browser to time out (PHP won't - I set_time_limit(0);).

Can someone provide sample code on how to make sure the browser doesn't time out (or else explain in detail what to do)? What's the best method? Ajax?

Thanks.

EDIT:

Brian Graham says, "Once javascript starts an ajax request, it runs until it finishes, quits or errors. As far as I know, it should even do this if the user leaves the page."

So, could I simply call the long PHP script via Ajax, and then it would run for however long is needed without ever timing out (even if nothing was returned by the script)?

解决方案

That really depends on what your script is doing and what it's sending back, but if you can use AJAX that would be the way to go. Not only would it let you run the request for a long period of time, but wouldn't tie up the browser leaving the user free to continue using the current page until the request is done.

The best way to implement something like this that I've found is to set ignore_user_abort and turn off the time limit. This way you can let the script run in the background. This way the script can continue to run while the user browses your site. They don't even have to stay on the same page. So you fire off the request with AJAX to start it up. Have a loop in that script that updates the progress to the user's session.

ini_set('session.use_cookies', 0); //don't spam the user with cookies for each session_start()

while( ... ) {

session_start();

//update status to the user's session

session_write_close();

//sleep or whatever

}

Obviously this doesn't have to be a loop. You can simply close the session before you start processing information, then open it, set a status, and close it again. Only one script can access a user's session at a time, so you need to make sure this process isn't tying that up.

Then all you need to do is make a script that runs on the pages of your site that lets the user know when whatever it is you're doing is done and/or provides them the file/output/etc.

This frees up the user to continue browsing your site while your script is running and doesn't force them to sit on a single page staring at a loading indicator.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值