I am getting the classic, "Cannot modify header information" when I try to set my cookies. The error is that headers already sent by (output started at .../myConn.php:14)

Here is my connection (myConn.php):

$LOCATION = 'localhost';
$USERNAME = 'username';
$PASSWORD = 'password';
$DATABASE = 'database';

// Connect to DB
$conn = mysql_connect($LOCATION,$USERNAME,$PASSWORD);
if (!$conn) die ("Cannot connect MySQL");	

// execute connection string
mysql_select_db($DATABASE,$conn) or die ("Could not open database");

The above works properly. I call that function into the following code as follows:


require('myConn.php');

# Set cookie
setcookie('username',$username,time()+432000,'/','http://www.websiteurl.com'); //Set for 5 days

I am not having problems setting cookies or connecting. Please disregard the fact that there may be variables missing. My problem is that I do not understand why I cannot set my cookies after connecting to the database with the above code. If I try to set the cookie before connecting, it works.

    Normally I would suspect my script, but I have used myConn.php through my entire program. It is included on multiple files and on the same pages as header() functions.

    I have never used cookies before, so I didn't know if there was something I should know when setting them.

    When it gives me the error message and puts the path with "myConn.php:14" at the end, what does the 14 represent? The line number? I am asking because my code and all my tags are less than 14 lines in the file myConn.php.

      yes, 14 is the line number

      if you look at the html source when you get the error message, whatever is before the error message, is your problem. likely its a blank line or a space.

        This is the code:

        
        <?php 
        
        //Config file holds all connection information
        require_once('config.php');  
        
        // Connect to DB
        $conn = mysql_connect(LOCATION,USERNAME,PASSWORD);
        if (!$conn) die ("Cannot connect MySQL");	
        
        // execute connection string
        mysql_select_db(DATABASE,$conn) or die ("Could not open database");
        
        ?> 
        
        

        This is the error:

        Warning: Cannot modify header information - headers already sent by (output started at /home/wheeler08/public_html/lib/base/myConn.php:10) in /home/wheeler08/public_html/application/account/login.php on line 42

        Line 42 is where I call the cookie:

        setcookie('username',$username,time() +432000,'/','http://websiteurl.com');
        

          (output started at /home/wheeler08/public_html/lib/base/myConn.php:10)

            Line 10 is where my PHP close tag is.....

              I have done that. I can't see where any output is created. As I said before, I have headers that work with myConn.php - if there was output, the headers wouldn't work.

                have you looked at the html source?

                there should be some sort of character before the php error message.

                if not, try retyping the last few lines of the file in question. maybe your editor inserted some garbage that you cant see.

                  Write a Reply...