PHP header()导致内部服务器错误

时间:2009-07-31 23:09:13

标签: php header

由于某种原因,对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]  

1 个答案:

答案 0 :(得分:9)

你正在使用这个:

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标题时应该使用FULL绝对URL;我知道使用相对URL工作(差不多?)所有浏览器,但是在读取HTTP RFC时它是无效的,如果我没记错的话。


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

在这种情况下,您应该检查error_log的文件......
...或者激活error_reportingdisplay_errors以促进事情 - 至少在您的开发机器上。

相关问题