【发布时间】:2016-04-26 18:07:38
【问题描述】:
调用后是否可以取消:
<?php
// Start output while creating profile
if (ob_get_level() == 0) ob_start();
for ($i = 0; $i<1; $i++)
{
// Redirect user and download the profile
header('Refresh: .02; download.php');
echo "You're profile is being created, please wait...";
ob_flush();
flush();
sleep(1);
}
ob_end_flush();
// Start creating the profile
// Start this after the code above due to the long execution time
if(!createProfile())
{
// If createProfile returns false, cancel redirect and stop execution
cancelRedirect();
// Inform the user about the problem
echo 'Error!';
exit;
}
?>
意图是调用header()-函数,然后脚本执行某些操作,如果结果不符合预期,则应取消重定向到“download.php”。
【问题讨论】:
-
从逻辑上讲,这是没有意义的。
-
这是唯一的方法,因为在调用
header()函数之前echo的某些内容不起作用。 -
虽然有
header_remove()。 -
我不能 100% 确定您要达到的目标,但输出缓冲可能是您的答案。
-
"这是唯一的方法" @alve89 - 不,肯定不是。正如下面的answer 中所指出的,如果满足某些条件,仅 发送您的标头更合乎逻辑。不要无条件地发送它们,然后尝试收回它们。关于在发送标头之前输出数据:也有解决该问题的简单方法,方法是运行任何/所有逻辑之前到
echo'ing 任何可能导致发送标头的东西。
标签: php header http-redirect cancellation