【问题标题】:PHP header() causes internal server error [duplicate]PHP header()导致内部服务器错误[重复]
【发布时间】:2009-07-31 23:09:13
【问题描述】:

由于某种原因,对 header() 的调用会导致我出现内部服务器错误。我正在使用 PHP5 并在此脚本中广泛使用 mod_rewrite(如果有帮助的话)。这是代码(有点):

<?php 

include 'core/initialize.php'; // Loads the core class (and session manager class)

    if($_GET['reset'] == 'true')
    {
         $core->Session->Visits = 0;
         header('Location', 'index.html');
         # header('X-Test', 'wtf'); // causes the error too :(
    }
    if(isset($core->Session->Visits)) $core->Session->Vists += 1;
    else $core->Session->Visits = 0;
    echo "Previous Visits: {$core->Session->Visits} (<a href='index.html?reset=true'>Reset</a>)";
?>

我的 .htaccess 文件如下所示:

# Start up the rewrite engine
Options +FollowSymLinks
RewriteEngine on

RewriteRule  ^(.*)$ navigator.php?nav=$1&%{QUERY_STRING} [NC]  

【问题讨论】:

    标签: php header


    【解决方案1】:

    你正在使用这个:

    header('Location', 'index.html');
    

    这不是header必须使用的方式:第二个参数应该是布尔值。

    并且第一个应该是标题名称+值。

    所以,在你的情况下,是这样的:

    header('Location: index.html');
    

    只有一个参数;和名称 + ':' + 值 :-)

    文档中有一个例子:

    第二种特殊情况是 “位置:”标题。 不仅如此 将此标头发送回浏览器, 但它也返回一个 REDIRECT (302) 状态码到浏览器,除非有些 3xx 状态码已经设置好了。

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


    作为旁注,如果我没记错的话,在使用 Location 标头时应该使用完整的绝对 URL;我知道使用相对 URL 在(几乎?)所有浏览器中都有效,但如果我没记错的话,在阅读 HTTP RFC 时它是无效的。


    作为第二个旁注(是的,答案已经被接受,但也许它还是有用的,下次:-)):“内部服务器错误”可能表明你的PHP 脚本遇到了某种错误。

    在这种情况下,您应该检查error_log 的文件...
    ...或者激活 error_reporting 和 display_errors 以方便操作 - 至少在您的开发机器上。

    【讨论】:

      猜你喜欢
      • 2011-01-22
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多