我想运行一个发送大量电子邮件(简报)的PHP脚本,但由于运行起来需要一段时间,我希望用户立即重定向到一个页面,其中包含“您的简报是”排队等待发送。“
到目前为止,标头重定向工作,但直到所有电子邮件发送后才会导致重定向。我可以在发送重定向后关闭连接,以便浏览器立即对重定向进行操作吗?我试过这样的东西
ob_start();
ignore_user_abort(true);
header( "refresh:1;url=waitforit.php?msg=Newsletters queued up, will send soon.");
header("Connection: close");
header("Content-Length: " . mb_strlen($resp));
echo $resp;
//@ob_end_clean();
ob_end_flush();
flush();
以各种排列和组合但无济于事。 我怀疑它不能这么简单,但在我开始搞乱cron作业或自定义守护进程之前,我想我会考虑这种方法。 感谢
例如下面提到的封锁头,但没有运气
ob_start();
ignore_user_abort(true);
header( "refresh:1;url=mailing_done.php?msg=Newsletters queued for sending.");
header("Connection: close");
header("Content-Length: 0" );
//echo $resp;
ob_end_flush();
flush();
答案 0 :(得分:15)
如果你想连续运行脚本并且仍然希望显示一些输出,为什么不使用ajax请求来服务器,它可以对邮件进行排队,并且仍然允许用户继续浏览该页面。
如果您不想使用此功能;相反,你可以使用,
即使用户将被重定向到show_usermessage.php
页面
<?PHP
//Redirect to another file that shows that mail queued
header("Location: show_usermessage.php");
//Erase the output buffer
ob_end_clean();
//Tell the browser that the connection's closed
header("Connection: close");
//Ignore the user's abort (which we caused with the redirect).
ignore_user_abort(true);
//Extend time limit to 30 minutes
set_time_limit(1800);
//Extend memory limit to 10MB
ini_set("memory_limit","10M");
//Start output buffering again
ob_start();
//Tell the browser we're serious... there's really
//nothing else to receive from this page.
header("Content-Length: 0");
//Send the output buffer and turn output buffering off.
ob_end_flush();
flush();
//Close the session.
session_write_close();
//Do some of your work, like the queue can be ran here,
//.......
//.......
?>
答案 1 :(得分:6)
我会试试这个:
<?php
header("Location: waitforit.php?msg=Newsletters queued up, will send soon.");
header("Content-Length: 0");
header("Connection: close");
flush();
//Do work here
?>
答案 2 :(得分:3)
这是正确的方法...... Sonia Desbiens的代码,又名:fataqui 07/01/2005
<?php
set_time_limit ( 0 );
/* start the forced redirect */
header ( 'Connection: close' );
ob_start ();
/* close out the server process, release to the client */
header ( 'Content-Length: 0' );
header ( 'Location: /page_to_redirect_to.php' );
ob_end_flush ();
flush ();
/* end the forced redirect and continue with this script process */
ignore_user_abort ( true );
/* the rest of your script here */
/*
* this example will redirect the user to '/page_to_redirect_to.php'
* then this script will continue by sleeping for '10' seconds, then
* this script will write a file, and then this script will exit...
*/
sleep ( 10 );
file_put_contents ( './out.txt', '' );
exit ();
?>
答案 3 :(得分:1)
尝试将Content-Length
设为0
答案 4 :(得分:1)
要在IIS7上使用此功能,您需要在web.config中使用带有responseBufferLimit="0"
的isapi,并在php.ini中使用output_buffering = Off,那么这样的东西应该可以工作......
session_write_close();
header( "Location: ".$sURL ) ;
// try to allow continued processing
if(ob_get_length())
ob_end_clean();
header("Connection: close");
ignore_user_abort(); // optional
ob_start();
header("Content-Length: 0");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
答案 5 :(得分:1)
接受的答案对我没有用,但确实如此。请注意,必须在php.ini
中打开ignore_user_abort()ignore_user_abort(true);
session_write_close(); // optional, this will close the session.
header("Location: $url");
header("Content-Length: 0");
ob_end_flush();
flush();
//do_function_that_takes_five_mins();
http://waynepan.com/2007/10/11/how-to-use-ignore_user_abort-to-do-process-out-of-band/
答案 6 :(得分:0)
基本上,您希望异步执行该作业。我希望php脚本创建另一个异步发送电子邮件的线程(或进程)。这样,运行请求的线程与电子邮件的处理无关。处理请求并返回结果的线程可以与发送电子邮件的线程完全分开运行。
这个主题似乎涉及到这种方法,或许对实现细节更有帮助:Run PHP Task Asynchronously