Hello. I've read suggestions regarding this error. Everyone says that the problem is that I am outputting to the browser (whitespace included) before sending a header. Apparently, this is unallowed. But I'm not outputting anything. Here's the only code that's in my index.php file:

<?php
error_reporting(E_ALL);

$eulacookie = $_COOKIE['eularead'];

if ($eulacookie == 'y')
{
	//redirect if cookie has been set
	header("Location: http://blahblahblah.com/hey.php");
}
else
{
	//redirect if cookie has NOT been set
	header("Location: http://blahblahblah.com/hey2.php");
}

?>

If I take the error_reporting() function out, everything works. But with that function in, I get the following error:

Notice: Undefined index: eularead in /home/virtual/site109/fst/var/www/html/index.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at /home/virtual/site109/fst/var/www/html/index.php:2) in /home/virtual/site109/fst/var/www/html/index.php on line 14

What's causing this? I'm not outputting anything to the browser, yet the error message that I'm getting kind of indicates this (according to previous posts that I looked at). Thanks.

    You are outputting to the browser. You're outputting "Notice: Undefined index: eularead in /home/virtual/site109/fst/var/www/html/index.php on line 2 ".

    By the time you try to send the header info, the above warning has already been sent to the browser, which is why you're getting the "headers already sent" error.

    You need to either reduce your error reporting to not display notices, or else you need to remove the cause of that notice. The following might work...

    if (isset( $_COOKIE['eularead'])){
        $eulacookie = $_COOKIE['eularead']; 
    }
    

      Cool. Thanks for the tip. I'll try it out as soon as my FTP connection comes back.

      Quick follow-up though, if I use the code that you show:

      if (isset( $_COOKIE['eularead']))
      {
      	$eulacookie = $_COOKIE['eularead'];
      }  

      Will I end up getting a similar message with the line:

      if ($eulacookie == 'y')
      

      Which will again cause that "Cannot modify header information" error later?

        hadoob024 wrote:

        Will I end up getting a similar message with the line:

        if ($eulacookie == 'y')
        

        Possibly. In any case it's not a good idea to leave uninitiated variables lying around the place. Better update my suggestion to:

        if (isset( $_COOKIE['eularead'])){
            $eulacookie = $_COOKIE['eularead']; 
        }else{
            $eulacookie = "N"; 
        }
        

          Cool. Yeah, that definitely seems to work better. But like you said, I just have to get back in the habit of initializing ALL variables, and not just ones passed through by POST, GET, COOKIE, etc. Thanks again...

            Write a Reply...