2009-07-31 77 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