Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP无法修改标题信息-标题已由发送_Php_Javascript_Html - Fatal编程技术网

PHP无法修改标题信息-标题已由发送

PHP无法修改标题信息-标题已由发送,php,javascript,html,Php,Javascript,Html,可能重复: 这是我的代码: <html> <body> <?php if($country_code == 'US') { header('Location: http://www.test.com'); } else { header('Location: http://www.test.com/'); } ?> <script languag

可能重复:

这是我的代码:

<html>
<body>
    <?php

    if($country_code == 'US')
    {
        header('Location: http://www.test.com');
    }

    else 
    {
        header('Location: http://www.test.com/');
    }

    ?>

<script language="JavaScript" type="text/javascript"></script>

</body>
</html>


我曾尝试将HTML代码和Javascript放在PHP下面,但这样做会使它无法跟踪页面的点击。

在标题之前不应输出任何内容。标题总是在内容之前发送。在调用
header()
之前,您不能输出任何内容,并且希望它能正常工作。(一些服务器可以启用输出缓冲,这可以解决问题,但它不能解决任何问题,也不好依赖。)


你关于跟踪页面点击的说明是胡说八道。大多数浏览器在给定带有
位置:
标题的
301
302
状态代码时,不会费心渲染HTML。

标题前不应输出任何内容。标题总是在内容之前发送。在调用
header()
之前,您不能输出任何内容,并且希望它能正常工作。(一些服务器可以启用输出缓冲,这可以解决问题,但它不能解决任何问题,也不好依赖。)


你关于跟踪页面点击的说明是胡说八道。大多数浏览器在使用位置为
301
302
状态代码时,不会费心渲染HTML。

在输出开始后无法发送“是”标题,类似的方法可以解决此问题

function redirect_to($url){
    // If the headers have been sent, then we cannot send an additional location header
    // so output a javascript redirect statement.
    if (headers_sent()){
       echo "<script>document.location.href='" . htmlspecialchars($url) . "';</script>\n";
    }else{
       header('HTTP/1.1 303 See other');
       header('Location: ' . $url);
    }
 }

 redirect_to('http://www.test.com/');
函数重定向到($url){
//如果标题已发送,则无法发送其他位置标题
//因此,输出一个javascript重定向语句。
如果(已发送的头){
echo“document.location.href=”.htmlspecialchars($url)。“;\n”;
}否则{
标题(“HTTP/1.1303见其他”);
标题('位置:'。$url);
}
}
将_重定向到('http://www.test.com/');

在输出开始后不能发送yes头,类似这样的方法可以解决此问题

function redirect_to($url){
    // If the headers have been sent, then we cannot send an additional location header
    // so output a javascript redirect statement.
    if (headers_sent()){
       echo "<script>document.location.href='" . htmlspecialchars($url) . "';</script>\n";
    }else{
       header('HTTP/1.1 303 See other');
       header('Location: ' . $url);
    }
 }

 redirect_to('http://www.test.com/');
函数重定向到($url){
//如果标题已发送,则无法发送其他位置标题
//因此,输出一个javascript重定向语句。
如果(已发送的头){
echo“document.location.href=”.htmlspecialchars($url)。“;\n”;
}否则{
标题(“HTTP/1.1303见其他”);
标题('位置:'。$url);
}
}
将_重定向到('http://www.test.com/');

如果您主要担心的是javascript跟踪代码,那么我建议使用javascript重定向:

<?php
// pre-html PHP code
?><!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="trackingScript.js"></script>
<script type="text/javascript">
<?php
if($country_code == 'US'){
  echo "document.location.href = 'http://www.test.com/1';";
}else{
  echo "document.location.href = 'http://www.test.com/2';";
}
?>
</script>
</head>
<body></body>
</html>


如果您主要担心的是javascript跟踪代码,那么我建议使用javascript重定向:

<?php
// pre-html PHP code
?><!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="trackingScript.js"></script>
<script type="text/javascript">
<?php
if($country_code == 'US'){
  echo "document.location.href = 'http://www.test.com/1';";
}else{
  echo "document.location.href = 'http://www.test.com/2';";
}
?>
</script>
</head>
<body></body>
</html>


您可以将PHP代码放在文件的开头

<?php
if($country_code == 'US')
{
    header('Location: http://www.test.com');
}
else 
{
    header('Location: http://www.test.com/');
}

?>
<html>
<body>

<script language="JavaScript" type="text/javascript"></script>

</body>
</html>

您可以将PHP代码放在文件的开头

<?php
if($country_code == 'US')
{
    header('Location: http://www.test.com');
}
else 
{
    header('Location: http://www.test.com/');
}

?>
<html>
<body>

<script language="JavaScript" type="text/javascript"></script>

</body>
</html>


您已经发送了
…是.html文件还是.php文件。如果它是一个html页面,浏览器就不能了解php。您的源代码必须在.php文件中才能执行php行。您已经发送了
…是.html文件还是.php文件。如果它是一个html页面,浏览器就不能了解php。您的源代码必须在.php文件中才能执行php行。这是怎么回事?我正在使用一个跟踪程序,Javascript是它用来跟踪点击的代码,顺便说一句,它已经工作了,只是与PHP结合有问题。这是胡说八道,因为服务器在给客户端浏览器任何东西之前处理PHP代码。因此,不要将
header()。标题在文档内容之前发送。在页面的中间做一个<代码>标题()/Cudio调用不会改变该头实际出现的位置。必须在内容之前发送标题。再说一次,我不知道您正在运行的JavaScript是什么,但大多数浏览器在重定向时都不会对HTML产生影响,您的JavaScript也不会运行。如果任何内容发送时带有
3xx
状态代码,它应该只是一条友好的消息,指示用户被重定向到的位置。谁否决了这一点,为什么?我已经受够了人们投票否决正确答案,却不解释原因。这怎么会是胡说八道呢?我正在使用一个跟踪程序,Javascript是它用来跟踪点击的代码,顺便说一句,它已经工作了,只是与PHP结合有问题。这是胡说八道,因为服务器在给客户端浏览器任何东西之前处理PHP代码。因此,不要将
header()。标题在文档内容之前发送。在页面的中间做一个<代码>标题()/Cudio调用不会改变该头实际出现的位置。必须在内容之前发送标题。再说一次,我不知道您正在运行的JavaScript是什么,但大多数浏览器在重定向时都不会对HTML产生影响,您的JavaScript也不会运行。如果任何内容发送时带有
3xx
状态代码,它应该只是一条友好的消息,指示用户被重定向到的位置。谁否决了这一点,为什么?我已经受够了人们投票否决正确答案,却没有解释原因。