简体   繁体   English

在输出用户消息后重定向PHP页面

[英]Redirect PHP page AFTER outputting user message

I am trying to redirect a page after successful execution. 我试图在成功执行后重定向页面。 However, I want to display a message to the user (eg 'Changed made. Redirecting...') while the redirection is done. 但是,我想在重定向完成时向用户显示一条消息(例如“Changed made.Redirecting ...”)。 Changing header variables after output in the page causes errors in PHP and I know this. 在页面输出后更改header变量会导致PHP出错,我知道这一点。 My question is, how should I do this? 我的问题是,我该怎么做?

My current code is something like 我目前的代码是这样的

// ... execution code
echo 'Changes made successfully. Now redirecting...';
header('Location: index.php');

and this doesn't work. 这不起作用。 I have also seen an answer on SO suggesting I use ob_start() and ob_flush() at the start and end of my page, respectively. 我也看到了SO的答案,建议我分别在页面的开头和结尾使用ob_start()ob_flush() But that didn't solve my problem either, I still get the error. 但这也没有解决我的问题,我仍然得到错误。

NB I need to use PHP only, I don't want JavaScript for redirection. NB我只需要使用PHP,我不希望JavaScript用于重定向。

Edit: I ended up using JavaScript for redirection, as I needed to output some useful message to the user before redirecting, and PHP alone isn't the best solution for that. 编辑:我最终使用JavaScript进行重定向,因为我需要在重定向之前向用户输出一些有用的消息,而单独使用PHP并不是最好的解决方案。

first, you cant use "header" after echo or any output. 首先,你不能在回声或任何输出后使用“标题”。 second, why you need php for it? 第二,为什么你需要PHP呢? you can use javascript or best, just put it in the html like it says here 你可以使用javascript或最好的,只需将它放在html中就像在这里说的那样

Echo after setting the header. 设置标题后回显。

header("refresh:1;url=test.php"); 
echo 'this is a test';
exit;
header( "refresh:5;url=wherever.php" );

You can redirect the page after 5 seconds to wherever.php. 您可以在5秒后将页面重定向到wherever.php。 Please see this question: Page redirect after certain time PHP 请看一下这个问题: 页面重定向一段时间后PHP

Drawback of redirection by all three popular methods 通过所有三种流行方法的重定向缺点

 header('Location:$url'); 
 echo "<meta http-equiv='refresh' content='3;$url>";
 echo "<body onload='window.location=$url'>";

is that you need to decide about possible redirection before any part of the page is displayed to user. 是您需要在向用户显示页面的任何部分之前决定可能的重定向。 When the decision process is time consuming, user will gawp at blank page. 当决策过程耗时时,用户将在空白页面上大喊大叫。 I use deferred redirection with Javascript OnLoad event, which is declared at the top of page but it does the actual redirection only if a hidden object with id='Redirect' is echoed anywhere later in the HTML body. 我使用Javascript OnLoad事件的延迟重定向 ,该事件在页面顶部声明,但只有当ID体'重定向'的隐藏对象稍后在HTML正文中的任何地方回显时才会执行实际重定向。

echo "<html><body onload='if (typeof(Redirect)!=undefined) 
setTimeout(function()
    {window.location=document.getElementById(\"Redirect\").value},3000);'>";
echo $HtmlBody; echo $FormToFill;flush();
if (CheckReceivedDataFailed()) echo "Wrong data, please fill the form again";
else echo "<input type='hidden' id='Redirect' value='$url'>
           Thanks, youll be redirected in 3 sec";
echo "</body></html>";

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

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