如何让浏览器超时(长PHP脚本) [英] How to keep browser from timing out (long PHP script)

查看:657
本文介绍了如何让浏览器超时(长PHP脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP脚本需要25秒就可以完成(1人),可能再与高流量。我不希望的浏览器的超时(PHP不会 - 我参数或者set_time_limit(0))。

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);).

有人可以提供的样品code 的关于如何确保浏览器不会超时(否则详细讲解该怎么办)?什么是最好的方法是什么?阿贾克斯?

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?

感谢。

编辑:

布赖恩·格雷厄姆说,一旦开始JavaScript的Ajax请求,它运行直到它完成,退出或错误。据我所知,它甚至应该做到这一点,如果用户离开页面。

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."

所以,我可以简单地通过Ajax调用长的PHP脚本,然后需要而没有超时,将运行但长期(即使什么也没有脚本返回)?

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)?

推荐答案

这真的取决于你的脚本是做什么它发回,但如果你能使用AJAX,这将是一段路要走。这不仅将让你运行了很长一段时间的请求,但不会占用浏览器留下可以继续使用当前页面,直至请求完成用户。

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.

要实现这样的事情,我已经找到了最好的方法是设置ignore_user_abort和关闭时间限制。这种方式可以让在后台运行脚本。通过这种方式,脚本可以继续当用户浏览你的站点上运行。他们甚至没有留在同一页上。所以,你火了与AJAX请求启动它。在该脚本更新进度到用户的会话一个循环。

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.

这篇关于如何让浏览器超时(长PHP脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆