2012-09-18 31 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()方法,它必须位于代码的顶部,然后才能执行任何其他输出(包括空格)。

相关问题