2009-07-31 74 views
4

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

<?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] 

回答

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头时使用完整的绝对URL;我知道在所有浏览器中(几乎?)使用相对URL,但在阅读HTTP RFC时,如果我正确记得,它是无效的。


作为第二阿里纳斯(是的,答案已经被接受,但也许这将是有用的,无论如何,下一次:-)):即“内部服务器错误”可能表明你的PHP脚本遇到了某种错误。

在这种情况下,你应该检查error_log的文件...
...或激活error_reportingdisplay_errors方便的事情 - 至少在开发机器上。

+0

现在我觉得很蠢。谢谢 – 2009-07-31 23:14:30