在后台执行 PHP 脚本...适合我的情况的最佳解决方案是什么?
我正在开发一个电子邮件系统,我必须为一家公司插入 CMS。我正在寻找一种方法,使电子邮件发送器脚本在后台运行,而管理员可以导航甚至关闭浏览器。
我发现了名为 ignore_user_abort()
的 PHP 函数,即使页面超时,也需要该函数来保持页面运行。
现在我有三个解决方案来“启动脚本”:
我可以使用 iframe
我可以使用 Ajax 调用,我之前已经将其配置为很早就超时。例如:使用 jQuery 框架 :
$.ajaxSetup({ timeout: 1000 });
< /p>- < p>我可以使用 cron 作业,但我想避免这种解决方案,因为它们是“虚拟的”并且在该服务器上不稳定。
还有其他解决方案吗?我不太喜欢 iframe 解决方案,但我已经将它们与 Ajax 上传器脚本一起使用。
我不希望管理员按 F5 并启动它的第二个实例。
该公司的用户被告知只能使用 Firefox 登录 CMS。 谢谢
I am working on a emailer system that I have to plug in a CMS for a company. I am looking for a way to make the email sender script run in background while the admin may navigate or even close the browsers.
I found the PHP function named ignore_user_abort()
which is required to keep the page running even if it has timed-out.
Now I have three solutions to "start the script" :
I could use an iframe
I could use an Ajax call which I have previously configured to timeout very early. Eg: using the jQuery framework :
$.ajaxSetup({ timeout: 1000 });
I could use a cron job but I would like to avoid this solution since they are "virtual" and are unstable on that server.
Is there any other solutions? I dont really like the iframe solution but I already use them with an Ajax uploader script.
I dont want the admin to hit F5 and start a second instance of it.
The company's users have been told to only log in the CMS using Firefox.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
Exec()
在后台运行 PHP 脚本。php 文档 提供了有关如何执行此操作的示例:
You can run a PHP script in the background using
Exec()
.php docs provide an example on how you can do that:
如果我是你,我会编写一个后台守护进程来执行此操作。我会将所有电子邮件保存到原始 HTTP 调用中的数据库中,并让一个脚本在后台不断运行,每隔 30 秒检查一次,看看是否有任何待处理的电子邮件,然后发送它们。
这还允许您在将电子邮件添加到队列时检查重复项,因此您不必担心多次添加。
编辑 鉴于您不使用 cron 作业的原因(即您没有对服务器的 shell 访问权限),我不会使用这种方法。
相反,我会进行第一个 AJAX 调用,其中 PHP 将所有电子邮件插入数据库,并将密钥返回给 Javascript。然后,我将使用该密钥进行 Javascript 调用,其中 PHP 处理电子邮件 5 秒钟,然后将剩余电子邮件数量返回给浏览器。然后我会让 Javascript 不断重复这个请求,直到没有剩余的电子邮件为止。
这还有一个额外的好处,即您可以获得进度通知,并且如果服务器由于某种原因崩溃,您的电子邮件将可以恢复。
If I were you, I would write a background daemon to do this. I would save all my emails to a database in the original HTTP call and have a script constantly running in the background that checks every, say, 30 seconds to see if there are any pending emails and then sends them.
This would also allow you to check for duplicates when you add emails to the queue, so you needn't worry about multiple additions.
Edit Given your reason for not using cron jobs (i.e. you don't have shell access to the server) I wouldn't use this approach.
Instead I would have a first AJAX call where the PHP inserted all the emails into a database, and returned a key to the Javascript. I would then have a Javascript call, using that key, where PHP processed emails for 5 seconds, then returned the number of emails remaining to the browser. I would then have Javascript keep repeating this request until there were no emails remaining.
This would have the additional benefit that you could have progress notifications and, if the server crashed for whatever reason, your emails would be recoverable.
仅仅因为您关闭浏览器,由 php 启动的脚本不应停止执行。因此,在我看来,您有几种方法可以做到这一点:
向服务器发送一个 POST 消息,告诉它您想要执行某些操作,然后脚本就会执行直到完成。重新加载页面将警告浏览器您需要重新发送数据等...您可以使用 javascript 异步执行 POST 以防止这种情况发生,并能够在脚本执行时执行其他操作。这里需要注意的一件事是脚本执行时间可能是有限的。
将 POST 发送到脚本,该脚本又启动后台系统进程执行相同的操作。但现在您将无法找出进度,除非您将状态保存在数据库或其他东西中
Cron(你似乎已经探索过这个选项)
Just because you close the browser the script started by php should not stop executing. So as I see it you have a couple of ways to do this:
Send a POST to the server telling it you want to do some action, and the script executes until it finishes. Reloading the page will warn the browser about that you will need to resend data etc... You could do the POST asynchronously with javascript to prevent this and to be able to do other things while the script is executing. One thing to watch out for here is that the script execution time might be limited.
Send a POST to a script that in turn starts a background system process doing the same thing. But now you will not be able to find out the progress unless you save the state somewhere, in a database or something
Cron (you already seem to have explored this option)