Php 标题已发送

Php 标题已发送,php,html,Php,Html,现在,如果我使用get变量one和two加载页面,它将重定向,但在重定向之前它会显示此php错误:警告:无法修改标题信息-标题已经由/home/site/public\u html/lp-19.php第5行/home/site/public\u html/lp-19.php中的/home/site/public\u html/lp-19.php:3发送 这是我的密码: <?php if(isset($_GET['one']) && isset($_GET['two']))

现在,如果我使用get变量
one
two
加载页面,它将重定向,但在重定向之前它会显示此php错误:
警告:无法修改标题信息-标题已经由/home/site/public\u html/lp-19.php第5行/home/site/public\u html/lp-19.php中的/home/site/public\u html/lp-19.php:3发送

这是我的密码:

<?php if(isset($_GET['one']) && isset($_GET['two'])) {?>

<?php
$value = "none";
setcookie("Disagree", $value, time()+30);
?>


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='//redirect.com/script.js'></script>
</head>
<body>
set "Disagree" cookie and then redirect
</body>
</html>

<?php } else { ?>

<?php
$value = "agree";
setcookie("Yes", $value, time()+3000000);
?>

<!DOCTYPE html>
<html>
set "yes" cookie and display content if no "one" or "two" get variables in url
</html>
<?php }?>

设置“不同意”cookie,然后重定向
如果url中没有“一个”或“两个”获取变量,则设置“是”cookie并显示内容
如何消除错误并让php根据加载的页面设置cookie?

试试这个;)


设置“不同意”cookie,然后重定向
如果url中没有“一个”或“两个”获取变量,则设置“是”cookie并显示内容

问题在于Cookie是在Response标头中发送的,必须在Response正文之前发送到浏览器。输出任何东西都会进入response体

您可以通过多种方式解决这个问题,包括输出缓冲,或者只是在输出代码之前运行所有将输出头的逻辑,但是这里您似乎只输出数据来执行javascript重定向

不要仅仅为了重定向而加载javascript,在php中设置重定向头,这也简化了代码:

<?php 
if(isset($_GET['one']) && isset($_GET['two'])) {
    $value = "none";
    setcookie("Disagree", $value, time()+30);
    header('Location: redirectUrlHere'); //<-set the correct url
    die();
}
$value = "agree";
setcookie("Yes", $value, time()+3000000);
?>

<!DOCTYPE html>
<html>
   ...
</html>

...
根据您的评论进行编辑:

<?php 
if(isset($_GET['one']) && isset($_GET['two'])) {
    $value = "none";
    setcookie("Disagree", $value, time()+30);
}else{
    $value = "agree";
     setcookie("Yes", $value, time()+3000000);
}

if($value=="none"):?>
    <!DOCTYPE html>
    <html>
        <head>
           <script type='text/javascript' src='//redirect.com/script.js'></script>
        </head>
        <body></body>
    </html>
<?php else:?>
    <!DOCTYPE html>
    <html>
        ...
    </html>
<?php endif;?>

...

这看起来很干净,但我需要javascript重定向,而不是php。。我将如何使用该代码?这需要时间在html标记之间加载内容,然后再重定向还是先重定向?@Joebbby这会立即重定向-为什么需要javascript重定向?因为它是从仪表板配置的外部脚本我准确粘贴了编辑过的版本,我得到了错误
Parse error:syntax错误,第10行的/home/site/public_html/lp-52.php中出现意外的“:”
@joebbby-Oops,在
$value==“none”
编辑以更正错误后,缺少结束括号
<?php 
if(isset($_GET['one']) && isset($_GET['two'])) {
    $value = "none";
    setcookie("Disagree", $value, time()+30);
}else{
    $value = "agree";
     setcookie("Yes", $value, time()+3000000);
}

if($value=="none"):?>
    <!DOCTYPE html>
    <html>
        <head>
           <script type='text/javascript' src='//redirect.com/script.js'></script>
        </head>
        <body></body>
    </html>
<?php else:?>
    <!DOCTYPE html>
    <html>
        ...
    </html>
<?php endif;?>