简体   繁体   English

PHP-处理完成后重定向PHP

[英]PHP - Redirect PHP after process done

I was wondering if it's possible to redirect a specific session after a background PHP script was done. 我想知道后台PHP脚本完成后是否可以重定向特定的会话。

header("location: index.php");

 $myboxes = $_POST['myCheckbox'];

 error_log("location:index.php");

 if(empty($myboxes))
 {
    error_log("You didn't select any boxes.");
  }
  else
  {
    $i = count($myboxes);
   error_log("You selected $i box(es): ");
    for($j = 0; $j < $i; $j++)
    {
      error_log($myboxes[$j] . " ");
    }
    header("location: sucess.php");
  }

Is it possible to redirect/refresh someone after i already redirect him (and waiting for the background process to finish running). 我已经将某人重定向(并等待后台进程完成运行)之后,是否可以重定向/刷新某人。

Many Thanks 非常感谢

My advice would be to run the script on index.php and then you can either display text or do a single redirect from there. 我的建议是在index.php上运行脚本,然后您可以显示文本或从此处进行单个重定向。

To answer your question directly, no it is not possible to send the headers twice. 要直接回答您的问题,否,不可能两次发送标题。 If you do no explicitly tell php to send the initial header it will wait till execution has finished and send the latest header, for example: 如果您没有明确告诉php发送初始标头,它将等待执行完成并发送最新标头,例如:

header("Location: initialPage.php");
sleep(2);
header("Location: secondPage.php");

Will wait 2 seconds and then redirect the user to 'secondPage.php' 将等待2秒钟,然后将用户重定向到“ secondPage.php”

Alternatively you could force php to send the headers by flushing the output like so: 或者,您可以通过刷新输出来强制php发送标头,如下所示:

header("Location: initialPage.php");

flush();
ob_flush();

sleep(2);

header("Location: secondPage.php");

Then the user will be redirected to initialPage.php straight away and it will ignore the second redirect 然后,用户将被立即重定向到initialPage.php,它将忽略第二个重定向

Conclusion: No you cannot get the effect you desire by sending 2 headers. 结论:不,您无法通过发送2个标头来获得想要的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM