例如,这是我的PHP代码:
<?php
if($country_code == 'US')
{
header('Location: http://www.test.com');
}
else
{
header('Location: http://www.test.com/');
}
?>我正在尝试使用Javascript代码进行跟踪,它必须在</body>标签之上。
我尝试过将PHP代码与HTML结合的不同方法,我也尝试过将其单独放在PHP下面,举个例子:
<html>
<head></head>
<body>
<?php
?>
<script></script>
</body>
</html>我得到的最远的结果是,它跟踪了点击,但没有重定向,给了我这个错误:
`Warning: Cannot modify header information - headers already sent by`感谢您的建议和帮助,谢谢!
发布于 2012-12-16 23:01:12
在输出任何内容之前,将php重定向代码放在文档的开头。检查?>标记之后和<?php标记之前的空格,因为这些空格将被打印出来,并且响应标头将被发送到那里,您将无法修改标头以进行重定向。
发布于 2012-12-16 22:57:52
您必须尝试执行以下操作才能进行跟踪:
<?php
if($country_code == 'US')
{
header('Location: http://www.test.com/?us=yes');
}
else
{
header('Location: http://www.test.com/?us=no');
}
?>然后在您的索引页面中检查us参数的值。此外,您还应该注意到在函数之前没有任何输出,以使警告无效:
Warning: Cannot modify header information - headers already sent by
发布于 2012-12-16 23:23:47
问题是PHP在JavaScript之前运行。所以你需要把PHP变量放在一个JavaScript里面。
<?php
// your normal code here, like connection to DB
?>
<script>var test = <?php> echo $thatVariable; <?> </script>https://stackoverflow.com/questions/13902366
复制相似问题