在输出用户消息后重定向PHP页面 [英] Redirect PHP page AFTER outputting user message
问题描述
我试图在成功执行后重定向页面。但是,我想在重定向完成时向用户显示一条消息(例如更改生成的重定向...)。在页面输出后,改变头部
变量导致PHP出错,我知道这一点。我的问题是,我应该如何做到这一点?
我现在的代码类似于
// ...执行代码
echo'更改成功。现在重定向...';
header('Location:index.php');
,这是行不通的。我也看到了一个答案,建议我在开始和结束时使用 ob_start()
和 ob_flush()
我的页面,分别。但是这也没有解决我的问题,我仍然得到错误。
NB我只需要使用PHP,我不希望JavaScript用于重定向。
编辑:我最终使用JavaScript重定向,因为我需要在重定向之前向用户输出一些有用的消息,而PHP本身并不是最好的解决方案。
header(refresh: 1; url = test.php);
echo'This is a test';
exit;
I am trying to redirect a page after successful execution. However, I want to display a message to the user (e.g. 'Changed made. Redirecting...') while the redirection is done. Changing header
variables after output in the page causes errors in PHP and I know this. 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. 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.
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.
Echo after setting the header.
header("refresh:1;url=test.php);
echo 'this is a test';
exit;
这篇关于在输出用户消息后重定向PHP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!