加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在php脚本完成之前导致重定向?

发布时间:2020-12-13 13:42:22 所属栏目:PHP教程 来源:网络整理
导读:我想运行一个 PHP脚本,它会发送一堆电子邮件(新闻通讯),但由于它可能需要一段时间才能运行,我希望用户立即被重定向到一个页面,其中包含“您的新闻通讯排队等待的消息”送出.” 到目前为止,标题重定向工作,但直到所有电子邮件发送后才会导致重定向.我可以在发
我想运行一个 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();
如果你想连续运行脚本并且仍然希望显示一些输出,为什么不使用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,//.......
    //.......
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读