How to cause a redirect to occur before php script finishes?(如何在 php 脚本完成之前导致重定向发生?)
问题描述
我想运行一个 php 脚本,它会发送一堆电子邮件(新闻通讯),但由于它可能需要一段时间才能运行,所以我希望用户立即被重定向到一个页面,其中包含类似您的新闻通讯是排队等待发送."
到目前为止,标头重定向有效,但直到所有电子邮件都发送完毕后才会导致重定向.我可以在发送重定向后关闭连接,以便浏览器立即执行重定向吗?我试过这样的东西
I want to run a php script that will send out a bunch of emails (newsletters) but since it can take a while for that to run I want the user be immediately redirected to a page with a message like "Your newsletters are queued to be sent out."
So far the header redirects work but they do not cause the redirection until after all the emails get sent. Can I close the connection after sending the redirect so the browser acts on the redirect immediately? I tried stuff like this
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 作业或自定义守护进程之前,我想我会研究这种方法.谢谢
in various permutations and combinations but to no avail. I suspect it cannot be done so simply but before I start messing with cron jobs or custom daemons I thought I'd look into this approach. Thanks
例如像下面建议的blockhead,但没有运气
eg like blockhead suggests below, but no luck
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();
推荐答案
如果你想连续运行脚本并且仍然想显示一些输出那么为什么不使用ajax请求到可以排队邮件的服务器并且仍然让用户继续浏览该页面.
If you want to run script continuously and still want to display some output then, why don't you use an ajax request to server which can queue mails and still let user continue browsing that page.
如果你不想用这个;相反,您可以使用,即使用户将被重定向到 show_usermessage.php
页面
If you don't want to use this; instead you could use,
the script runs background even if the user will be redirected to show_usermessage.php
page
<?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,
//.......
//.......
?>
这篇关于如何在 php 脚本完成之前导致重定向发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 php 脚本完成之前导致重定向发生?
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01