我正在编写一个php脚本,用于将邮件发送到特定邮件列表中的所有电子邮件地址。 邮件可能会达到数千个,整个脚本可能需要几个小时才能执行。 我想要的是一旦用户输入邮件详细信息,正文,主题等并点击按钮/链接,php脚本在后台运行,用户在点击按钮/链接后立即重定向到某个页面。</ p>

简而言之,php脚本应该从浏览器启动,但即使浏览器关闭也应该在服务器上运行。</ p>

或者如果进度条可以 使用AJAX / Javascript在重定向页面上显示,但即​​使浏览器关闭,php脚本也应该继续执行。</ p>
</ div>



I am writing a php script to send mails to all the email addresses in a particular mailing list. The mails might run into thousands and the entire script can take even hours to execute. What I want is the once the user enter the mail details, body, subject etc and hits a button/link, the php script runs in background and the user is redirected to some page as soon as he hits the button/link.

In short the php script should be initiated from the browser but should run on the server even if the browser is closed.

Alternatively if a progress bar can be shown on redirected page using AJAX/Javascript but the php script should continue executing even if the browser is closed.


PHP是错误的工作工具</ strong>。 在电子邮件服务器(可能是同一台服务器)上设置邮件列表,然后从浏览器通过ajax将数据传递给PHP,然后应该发送电子邮件(仅一次</ strong>)到 邮件列表。</ p>

电子邮件软件将负责向邮件列表的每个订阅者发送一份副本。</ p>

这是其中一个 最有效</ strong>的方法。</ p>

一些可能有助于您进行更多研究的链接:</ p>



PHP is the wrong tool for the job. Set up a mailing list on a e-mail server (perhaps the same server), and from the browser hand the data to PHP via ajax, which should then send the e-mail (only once) to the mailing list.

The e-mail software will take care of sending a copy to every subscriber of the mailing list.

This is one of the most efficient ways of doing it.

Some links which may help you do more research:

Remember though, this has nothing to do with programming, and you should ask for help on some other stack exchange site (like unix or serverfault).

要使脚本继续运行并让它给它状态,即使用户刷新它也有点工作。</ p >

如果您只需要执行服务器端的操作,您可以拥有XHR(也称为ajax)并使用长轮询方法提供进度 。</ p>

如果您希望进程独立于用户连接,则每次都需要生成一个新的PHP实例。 然后找到一种方法将其输出发送给用户。
您可以将输出存储到redis中。例如。</ p>
</ div>



To get both the script to continue and having it giving it status even though the user refresh this is a bit of work.

The super easy approach is to drop the the second part. If you just need a action to be executed server side you could have a XHR (aka ajax) and give the progression with a long polling approach.

If you want the process to be independent from the user connection you need to spawn a new PHP instance each time. and then find a way to get its output to the user. You could store the output into a redis for example.

dqyin0101 如果你走这条路,当你达到1000分时,无论如何你都会回到我的答案。 基本上,你试图以某种方式使用软件(这里是:PHP)的任何方式都不会被用来杀死你的服务器;)
大约 8 年之前 回复
dongzhansong5785 所以如果我放弃显示进度条,只是希望该脚本完成执行即使浏览器关闭,还需要做什么?
大约 8 年之前 回复

您要做的是以下内容:</ p>

点击按钮调用您的处理 脚本。 然后,处理脚本应使用 ignore_user_abort 来处理请求 一旦浏览器继续运行,脚本就会停止,然后使用标题重定向更大的标题(' 位置:success.html'); </ p>



</ code> </ pre>

但是,您需要确保脚本没有激活输出缓冲。 如果您正在使用某些压缩模块或任何可能在提交内容之前等待整个过程完成的任何内容,从而冻结整个内容。 最好是禁用脚本URL的任何输出缓冲。</ p>

这类似于在PHP中创建守护进程的方式。</ p>
</ div>



What you want to do is the following:

On click of the button call your processing script. The processing script should then handle the request by using ignore_user_abort to prevent the script from stopping once the browser moves on and then redirecting the broser with a header('Location: success.html');

  header('Location: success.html');
  echo 'Making sure the header gets flushed';

  //Send out the mails

However you will need to make sure there is no output buffering active for the script. If you're using some compression modules or anything those might wait for the whole process to finish before delivering the content and therefore freezing the whole thing. Best is to disable any output buffering for the script url.

This is similar to how you would create a deamon in PHP.

Csdn user default icon