【问题标题】:PHP: header() and include/include_once/require/require_oncePHP: header() 和 include/include_once/require/require_once
【发布时间】:2012-07-08 09:38:36
【问题描述】:

我正在调用 header('Location: http://foo.com/foo/');在使用包含函数调用(无输出)的文件调用 include() 之后的文件中。每当我调用 include() 时,PHP header() 重定向都不起作用。这只发生在服务器上。无论我包含哪个 PHP 文件,都会发生这种情况。 (本地文件在开发过程中上传到服务器。)


实际上我在 DOCTYPE 声明之前得到了一个空白字符, 这是非常糟糕的!无论我包含一个还是两个文件,我都会得到一个空白字符。

    Neil@NEILCOMPUTER /c/wamp/www/workspace/worknet
    $ curl --verbose http://timescapezonecom.ipage.com/worknet/foo.php
    * About to connect() to timescapezonecom.ipage.com port 80 (#0)
    *   Trying 66.96.147.110... connected
    * Connected to timescapezonecom.ipage.com (66.96.147.110) port 80 (#0)
    > GET /worknet/foo.php HTTP/1.1
    > User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8k zlib/1.2.3
    > Host: timescapezonecom.ipage.com
    > Accept: */*
    >
    < HTTP/1.1 302 Found
    < Date: Sun, 08 Jul 2012 23:19:12 GMT
    < Content-Type: text/html; charset=iso-8859-1
    < Content-Length: 206
    < Connection: close
    < Server: Nginx / Varnish
    < X-Powered-By: PHP/5.2.17
    < Set-Cookie: PHPSESSID=0310603f03176770c3feb46ea93d9fd3; path=/
    < Expires: Thu, 19 Nov 1981 08:52:00 GMT
    < Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    < Pragma: no-cache
    < Location: http://www.google.com
    <
     <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>302 Found</title>
    </head><body>
    <h1>Found</h1>
    <p>The document has moved <a href="http://www.google.com">here</a>.</p>
    </body></html>
    * Closing connection #0

另一方面,当我在 foo.php 中不包含任何文件时,我在之前得到一个零 doctype,我也觉得很奇怪:

    Neil@NEILCOMPUTER /c/wamp/www/workspace/worknet 
    $ curl --verbose http://timescapezonecom.ipage.com/worknet/foo.php
    * About to connect() to timescapezonecom.ipage.com port 80 (#0)
    *   Trying 66.96.147.110... connected
    * Connected to timescapezonecom.ipage.com (66.96.147.110) port 80 (#0)
    > GET /worknet/foo.php HTTP/1.1
    > User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8k zlib/1.2.3
    > Host: timescapezonecom.ipage.com
    > Accept: */*
    >
    < HTTP/1.1 302 Found
    < Date: Sun, 08 Jul 2012 23:25:28 GMT
    < Content-Type: text/html; charset=iso-8859-1
    < Content-Length: 206
    < Connection: close
    < Server: Nginx / Varnish
    < X-Powered-By: PHP/5.2.17
    < Set-Cookie: PHPSESSID=98c60264dbcdd024d91bae25db848186; path=/
    < Expires: Thu, 19 Nov 1981 08:52:00 GMT
    < Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    < Pragma: no-cache
    < Location: http://www.google.com
    <
    0<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>302 Found</title>
    </head><body>
    <h1>Found</h1>
    <p>The document has moved <a href="http://www.google.com">here</a>.</p>
    </body></html>
    * Closing connection #0

这是 foo.php 文件:

    <?php
        session_start();

        // I added the require just to make sure nothing is being printed there
        // (an error for example)
    //    require_once('include/connect.php');
    //   require_once('include/util.php');

        header("Location: " . "http://www.google.com");
        exit;
    ?>

我真的很想知道文件开头的 0 是从哪里来的。

【问题讨论】:

  • 你是在你的标题后面直接打电话给exit()/die()吗?可能正在生成一些输出并阻止它。
  • 是的,在标头之后我正在调用退出,这样就不会将任何输出发送到浏览器。这是 PHP 站点上的示例的编写方式。这种方法有什么问题?
  • 没什么,你应该这样做。我只是想确定:)
  • 尝试使用curl 命令准确检查您的网页返回的内容。我会使用curl --verbose http://url。然后在这里发布你的结果。仅供参考,PHP 手册说“空白行”可以阻止此重定向工作。 curl 输出应准确显示您的网页正在生成的内容。
  • 如果您在包含文件中使用任何 php 结束标记 ?&gt;,请确保您没有任何空格。

标签: php redirect include require


【解决方案1】:

强烈建议不要对非模板文件使用 php 的结束标记。

<?php //VERY first line, no whit space
  // code
  // NO ?> *anywhere*

通常是 ?> 之后的换行符导致您的错误。

【讨论】:

  • 好的,这很有趣。你在哪里找到这些信息的?我认为有一个开放标签但没有结束标签在语法上是不正确的。我很困惑。
  • 在 PHP 手册中,“如果文件是纯 PHP 代码,最好在文件末尾省略 PHP 结束标记。这样可以防止在PHP 结束标记,这可能会导致不必要的影响,因为当程序员无意在脚本中的该点发送任何输出时,PHP 将开始输出缓冲。” php.net/manual/en/language.basic-syntax.phptags.php
  • 将结束标签更多地视为“停止执行”命令而不是需要“关闭您打开的内容”会很有帮助。
  • 好吧,我仍然对这种情况不满意。因为我已经注释掉了我包含的 php 文件中的所有结束标记,但是现在,我没有在
  • 所以现在我们离解决问题又近了一步。如果能找出那个“零”字符存在的原因就好了。再一次,它只发生在 ipage.com 服务器上。
猜你喜欢
  • 2011-12-08
  • 2011-01-25
  • 2011-04-02
  • 2018-01-18
  • 2011-08-28
相关资源
最近更新 更多