如何在php脚本完成前导致重定向? [英] How to cause a redirect to occur before php script finishes?

查看:83
本文介绍了如何在php脚本完成前导致重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个php脚本来发送一堆电子邮件(通讯),但由于它需要一段时间才能运行,我希望用户可以立即重定向到一个带有类似你的通讯是排队等待发送。

到目前为止,标题重定向工作,但直到所有电子邮件发送完毕后才会导致重定向。我可以在发送重定向后关闭连接,以便浏览器立即执行重定向操作吗?我试过这样的东西

  
ob_start();
ignore_user_abort(true);
header(refresh:1; url = waitforit.php?msg =通讯排队,很快就会发送。);
header(Connection:close);
header(Content-Length:。mb_strlen($ resp));
echo $ resp;
// @ ob_end_clean();
ob_end_flush();
flush();

在各种排列和组合中,但无济于事。
我怀疑它不能这么简单,但在开始搞定cron作业或自定义守护进程之前,我想我会研究这种方法。
谢谢



例如像下面的blockhead建议,但没有运气

  ob_start(); 
ignore_user_abort(true);
header(refresh:1; url = mailing_done.php?msg =通讯排队等待发送。);
header(Connection:close);
header(Content-Length:0);
// echo $ resp;
ob_end_flush();
flush();

解决方案

如果您想连续运行脚本并仍想然后显示一些输出,为什么不使用ajax请求服务器可以排队邮件,并仍然让用户继续浏览该页面。



如果你不不想用这个;相反,您可以使用>即使用户将被重定向到 show_usermessage.php 页面


,脚本仍会运行后台

 <?PHP 
//重定向到另一个显示邮件排队的文件
header(Location:show_usermessage.php);

//擦除输出缓冲区
ob_end_clean();

//告诉浏览器连接的
头(Connection:close);

//忽略用户的中止(我们通过重定向导致)。
ignore_user_abort(true);
//将时间限制延长至30分钟
set_time_limit(1800);
//将内存限制扩展到10MB
ini_set(memory_limit,10M);
//重新开始缓冲输出
ob_start();

//告诉浏览器我们很认真......真的有
//没有别的东西可以从这个页面接收。
header(Content-Length:0);

//发送输出缓冲区并关闭输出缓冲。
ob_end_flush();
flush();
//关闭会话。
session_write_close();


//做一些工作,比如队列可以在这里跑,
// .......
// ... ....
?>


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();

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

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(); 

解决方案

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.

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脚本完成前导致重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆