【问题标题】:Cancel a header(refresh)取消标题(刷新)
【发布时间】: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


【解决方案1】:

这是不可能的,但反过来是可能的:

<?php
    if (doSomething()) {
        header('Refresh: 1; download.php');
    }
?>

【讨论】:

  • 当然这是可能的——但不幸的是这对我没有帮助。用 JavaScript 可以做到吗?
  • 看起来这是一个 X-Y 问题...告诉我们完整的问题,以便我们可以为您提供更好的指导。 @alve89...
  • 我尝试:我正在创建一个包含邮件帐户、日历帐户等数据的配置文件。这非常耗时,所以我想通知用户配置文件是正在创建(这比加载空白页面要好得多,不是吗?)。成功创建配置文件后,我想将用户重定向到下载文件的页面。我还编辑/更新了主要问题。这可以理解吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-05
  • 2012-02-03
  • 2011-06-23
  • 2015-11-23
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多