繁体   English   中英

PHP:header()和include / include_once / require / require_once

[英]PHP: header() and include/include_once/require/require_once

我正在调用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是从哪里来的。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM