【问题标题】:Why does a PHP redirect load the page before actually redirecting?为什么 PHP 重定向会在实际重定向之前加载页面?
【发布时间】:2016-09-24 01:47:27
【问题描述】:

我有一个页面需要几秒钟才能加载,并使用 PHP 标头(“位置:”)设置登录检查/重定向。当以注销用户身份访问该页面时,我注意到实际重定向到登录页面之前需要几秒钟,并假设它首先呈现页面的完整代码。但是既然重定向需要在其他任何事情之前渲染,为什么它会继续执行页面代码的其余部分呢?

【问题讨论】:

    标签: php


    【解决方案1】:

    您应该在header() 位置调用之后立即拥有exit() 以停止页面处理的任何其余部分。

    手册中的示例

    <?php
    header("Location: http://www.example.com/"); /* Redirect browser */
    
    /* Make sure that code below does not get executed when we redirect. */
    exit;
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-24
      • 2015-02-23
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多