【问题标题】:PHP redirection returns "Cannot modify header information"PHP 重定向返回“无法修改标头信息”
【发布时间】:2012-09-18 09:42:44
【问题描述】:

我目前有一个非常简单的页面,可以重定向到另一个 URL。但是由于某种原因,我无法让它工作。

我确信有一个非常简单的解决方案可以解决这个问题,如果能帮助我准确了解正在发生的事情,我们将不胜感激。

这是我的页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test - Youtube</title>
</head>
<body>
    <?php 
    ob_start();
    header('Location: vnd.youtube:xjTEqVQVsQs'); 
    ob_end_flush();
    ?>
</body>
</html>

谢谢

【问题讨论】:

  • 这是每个php新手的问题。做一些研究:)
  • 放退出;重定向后。
  • 不要在标题前输出!在header之前使用ob_clean()
  • 您想重定向到 youtube 视频页面吗?

标签: php url redirect


【解决方案1】:

在发送标头之前,您的页面上有一些输出,这就是您无法重定向的原因,您应该像这样使用输出缓冲:

<?php

ob_start();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test - Youtube</title>
</head>
<body>

<?php 

header('Location: vnd.youtube:xjTEqVQVsQs');
die();

?>

</body>
</html>

<?php

ob_end_flush();

?>

【讨论】:

    【解决方案2】:
    <?php 
    ob_start();
    header('Location: vnd.youtube:xjTEqVQVsQs'); 
    ob_end_flush();
    ?>
    

    如果它只是一个重定向页面,则删除除上述 php 代码之外的所有 html。

    【讨论】:

    • 事实上你也不需要 ob_start 或 ob_end_flush
    【解决方案3】:

    代码应该是这样的:

       <?php 
        ob_start();
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Test - Youtube</title>
        </head>
        <body>
        <?php
        header('Location: vnd.youtube:xjTEqVQVsQs'); // Are you sure that's the right 
    address?
        ?>
        </body>
        </html>
        <?php
        ob_end_flush();
        ?>
    

    反正页面是重定向到另一个页面,为什么需要html标签?

    【讨论】:

      【解决方案4】:

      您应该删除除 PHP 脚本之外的所有页面内容:

      <?php 
      ob_start();
      header('Location: vnd.youtube:xjTEqVQVsQs'); 
      ob_end_flush();
      ?>
      

      或者

      将您的 PHP 脚本放在页面顶部:

      <?php 
          ob_start();
          header('Location: vnd.youtube:xjTEqVQVsQs'); 
          ob_end_flush();
      ?>
      

      确保上面的 php 标签前没有空格。

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Test - Youtube</title>
      </head>
      <body>
      </body>
      </html>
      

      【讨论】:

        【解决方案5】:

        当重定向向浏览器发送标头时,它必须在任何输出之前发送。 您可以使用以下方法根据是否已发送标头使用不同的方法进行重定向:

        <?php
        function redirect($filename) {
            if ( ! headers_sent())
                header('Location: '.$filename);
                exit; // just a good practice to EXIT after redirecting
            else {
                echo '<script type="text/javascript">';
                echo 'window.location.href="'.$filename.'";';
                echo '</script>';
                echo '<noscript>';
                echo '<meta http-equiv="refresh" content="0;url='.$filename.'" />';
                echo '</noscript>';
            }
        }
        redirect('http://www.google.com');
        ?>
        

        如果标头已经发送,这将回退到使用 javascript。

        如果您想使用header() 方法,它必须在任何其他输出(包括空格)之前位于代码的顶部。

        【讨论】:

          猜你喜欢
          • 2018-05-22
          • 2014-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-14
          • 2012-06-19
          • 2011-08-03
          相关资源
          最近更新 更多