0

I have an 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');
?>

And 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();
 }

?>

After I load index.php I receive an error

Warning: Cannot modify header information - headers already sent by (output started at /mounted-storage/home93c/sub006/sc36756-TMHW/xxxxx.com/onarter/index.php:8) in /mounted-storage/home93c/sub006/sc36756-TMHW/site.com/onarter/init.php on line 42. How can I get rid of this problem?

Florent
  • 12,310
  • 10
  • 49
  • 58
user1614240
  • 115
  • 2
  • 7
  • Make sure there is no trailing whitespace (even a return) after the last ?> in init.php - in fact, you can get rid of the ?> completely – Mark Baker Sep 13 '12 at 12:02

3 Answers3

6

You are trying to send a cookie after headers have already been sent (and by headers, I mean anything) - this also covers the header() in the code further down. Anything sent to the user and it borks it as well.

What you need to do is include the init.php BEFORE any other output.

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

If your init.php had:

<?php

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

That would also kill the setcookie/header further in your code. You cannot sent anything as output before calling these sort of functions.

Florent
  • 12,310
  • 10
  • 49
  • 58
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
1

The header() function only works when nothing else has been done by your script. You are setting cookies in your script, which sends headers to the user, and later you are trying to send headers again.

Andy Ibanez
  • 12,104
  • 9
  • 65
  • 100
1

You can not use header function ( you can but all it will do is output an error ) after echoing something, or html. You have to put it before any echo or html in your script.

Kuro
  • 878
  • 9
  • 28