PHP重定向返回“;无法修改标题信息";
我现在有一个非常简单的页面,可以重定向到另一个URL。然而,由于某种原因,我无法让它工作 我相信有一个非常简单的解决方案来解决这个问题,任何能让我确切了解情况的帮助都将不胜感激 这是我的页面:PHP重定向返回“;无法修改标题信息";,php,url,redirect,Php,Url,Redirect,我现在有一个非常简单的页面,可以重定向到另一个URL。然而,由于某种原因,我无法让它工作 我相信有一个非常简单的解决方案来解决这个问题,任何能让我确切了解情况的帮助都将不胜感激 这是我的页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://ww
<!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>
测试-Youtube
谢谢代码应该是这样的:
<?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();
?>
测试-Youtube
如果只是一个重定向页面,请删除除上述php代码之外的所有html 在发送标题之前,您的页面上有一些输出,这就是为什么您不能重定向,您应该像这样使用输出缓冲:
<?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();
?>
测试-Youtube
您应该删除除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>
测试-Youtube
当重定向将标题发送到浏览器时,它必须在任何输出之前发送。
您可以使用以下方法使用不同的方法重定向,具体取决于是否发送了标头:
<?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
如果您想使用该方法,它必须在任何其他输出(包括空格)之前位于代码顶部。这是每个php新手的问题。对此做一些研究:)退出;重定向后。不要在标题前输出!在标题之前使用ob\u clean()
是否要重定向到youtube视频页面?事实上,您也不需要ob\u start或ob\u end\u flush
<?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');
?>