2012-09-18 29 views
0

我目前有一個非常簡單的頁面,可以重定向到另一個URL。然而由於某種原因,我無法得到這個工作。PHP重定向返回「無法修改標題信息」

我敢肯定,這個問題有一個非常簡單的解決方案,任何幫助,讓我明白到底發生了什麼,將不勝感激。

這裏是我的網頁:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Test - Youtube</title> 
</head> 
<body> 
    <?php 
    ob_start(); 
    header('Location: vnd.youtube:xjTEqVQVsQs'); 
    ob_end_flush(); 
    ?> 
</body> 
</html> 

感謝

+0

這是每個php新手的問題。做一些研究如此:) – Rinzler

+0

把退出;重定向後。 – Rinzler

+0

不要在標題之前輸出!在'header'前面使用'ob_clean()' –

回答

1

您在發送頭之前你的頁面上的一些輸出,這就是爲什麼你不能重定向,您應該使用輸出緩衝這樣的:

<?php 

ob_start(); 

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Test - Youtube</title> 
</head> 
<body> 

<?php 

header('Location: vnd.youtube:xjTEqVQVsQs'); 
die(); 

?> 

</body> 
</html> 

<?php 

ob_end_flush(); 

?> 
0

這是代碼看起來應該像:

<?php 
    ob_start(); 
    ?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Test - Youtube</title> 
    </head> 
    <body> 
    <?php 
    header('Location: vnd.youtube:xjTEqVQVsQs'); // Are you sure that's the right 
address? 
    ?> 
    </body> 
    </html> 
    <?php 
    ob_end_flush(); 
    ?> 

無論如何,頁面重定向到另一個頁面,你爲什麼需要html標籤?

1
<?php 
ob_start(); 
header('Location: vnd.youtube:xjTEqVQVsQs'); 
ob_end_flush(); 
?> 

刪除除上述php代碼之外的所有html,如果它只是一個重定向頁面。

+0

事實上,你不需要ob_start或ob_end_flush – RiggsFolly

0

您應該刪除所有頁面內容除了PHP腳本:

<?php 
ob_start(); 
header('Location: vnd.youtube:xjTEqVQVsQs'); 
ob_end_flush(); 
?> 

或者

把你的PHP腳本在頁面頂部:

<?php 
    ob_start(); 
    header('Location: vnd.youtube:xjTEqVQVsQs'); 
    ob_end_flush(); 
?> 

確保在上面的php標籤之前沒有空的空間。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Test - Youtube</title> 
</head> 
<body> 
</body> 
</html> 
0

由於重定向向瀏覽器發送標題,它必須在任何輸出之前發送。 您可以使用下面的方法使用取決於頭中是否已經發出了不同的方法來重定向:

<?php 
function redirect($filename) { 
    if (! headers_sent()) 
     header('Location: '.$filename); 
     exit; // just a good practice to EXIT after redirecting 
    else { 
     echo '<script type="text/javascript">'; 
     echo 'window.location.href="'.$filename.'";'; 
     echo '</script>'; 
     echo '<noscript>'; 
     echo '<meta http-equiv="refresh" content="0;url='.$filename.'" />'; 
     echo '</noscript>'; 
    } 
} 
redirect('http://www.google.com'); 
?> 

這將回退到使用JavaScript如果頭部已經發送。

如果您想使用header()方法,它必須位於代碼的頂部,然後才能執行任何其他輸出(包括空格)。

相關問題