由于某种原因,调用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>)";
?>
Run Code Online (Sandbox Code Playgroud)
我的.htaccess文件如下所示:
# Start up the rewrite engine
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ navigator.php?nav=$1&%{QUERY_STRING} [NC]
Run Code Online (Sandbox Code Playgroud)
你正在使用这个:
header('Location', 'index.html');
Run Code Online (Sandbox Code Playgroud)
它不是header必须使用的方式:第二个参数应该是布尔值.
第一个应该是标题名称+值.
所以,在你的情况下,这样的事情:
header('Location: index.html');
Run Code Online (Sandbox Code Playgroud)
只有一个参数; 和名称+':'+值:-)
文档中有一个例子:
第二个特例是"Location:"标题.它不仅将此标头发送回浏览器,而且还向浏览器返回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;
?>
Run Code Online (Sandbox Code Playgroud)
作为旁注,如果我没记错的话,在使用Location标题时应该使用FULL绝对URL ; 我知道使用相对URL工作(几乎?)所有浏览器,但是在读取HTTP RFC时它是无效的,如果我没记错的话.
作为第二个旁注(是的,答案已被接受,但也许它将是有用的,下次:-)):"内部服务器错误"可能表明您的PHP脚本遇到了某种错误.
在这种情况下,你应该检查error_log文件......
或者激活error_reporting和display_errors促进事情 - 至少在你的开发机器上.