Hello,

I'm receiving the following error when I try to add a cookie in one of my scripts:

Warning: Cannot add header information - headers already sent by (output started at /usr/www/users/tnevni/menu1.php:19) in /usr/www/users/tnevni/protection2.php on line 126

Line 126 is the last line of the cookie statement "time()"

I believ their is some kind of confusion between the header in Menu1.php and the protection2.php script trying to create the cookie.

Any thoughts on how I can work around or fix this?

Thanks very much,

Gary

------------------------------------------------- protection2.php
if ($remember == "1") {
$key = md5(uniqid(microtime()));
$query = "SELECT * FROM client_keys WHERE client_email_address = '$login_email_address'";
$result = mysql_query($query) or die ("Can't connect [ 003 ] because ".mysql_error());
$row = mysql_fetch_assoc($result);
$client_key_id = $row["id"];

			if (mysql_num_rows($result)==1) {
               echo "client_key_id: " . "$client_key_id" . "<br>";
               echo "key: " . "$key" . "<br>";
	           $query="UPDATE client_keys SET client_key = '$key' WHERE id='$client_key_id'";
               mysql_query($query) or die ("Can't connect because [ 005 ]".mysql_error());
			   setcookie('h4hcookie',  // the name of the cookie
               $key,                   // the cookie value
               time()+60*60*24*30); }  // when the cookie will expire (= after 30 days)

			else {
               echo "key: " . "$key" . "<br>";
               $query = "INSERT INTO client_keys VALUES ('', '$login_email_address', '$key')";
			   mysql_query($query) or die ("Can't connect because [ 004 ]".mysql_error()); 
			   setcookie('h4hcookie',  // the name of the cookie
               $key,                   // the cookie value
               time()+60*60*24*30); }  // when the cookie will expire (= after 30 days)

         }   

The menu1.php script has the following code at the very top of the page:
------------------------------------------ menu1.php
<?php
$todaydate = date("l | F j, Y");

$action = "";
$client_url = "";
extract( $POST );
extract( $
GET );

$menu = $client_url;
if ($menu != "")
{
header("Location: $client_url");
exit;
}
else
{
?>
<?php
/ -------------------------- Initialize Variables -------------------------- /
$user="xxx";
$password="xxx";
$database="xxx";
$host="xxx";
mysql_connect($host,$user,$password) or die ("Can't connect because [ 001 ]".mysql_error());
@mysql_select_db($database) or die ("Can't connect because [ 002 ]".mysql_error());

// check for a 'h4h cookie'
if (isset($_COOKIE['h4hcookie'])) {
// there is a cookie, lookup username
$result = mysql_query('SELECT client_email_address FROM client_keys' . 'WHERE client_key = "'.$key.'";');

// is there such a key in the database?
if (mysql_num_rows($result)==1) {
$login_email_address = mysql_result($result, 0, 'client_email_address');
// this user is logged in as $login_email_address
include("protection2.php");
}
}

include("protection2.php");

.
.
.

    It because you have already output this

    echo "key: " . "$key" . "<br>";

    to the browser.

    Set the cookie before you echo anything to the screen and you'll be OK or try looking at ob_start() to find out how to use output buffering

    hth

    gm

      Well I removed all the echos from the first script and I still receive the following error:

      Warning: Cannot add header information - headers already sent by (output started at /usr/www/users/tnevni/menu1.php:19) in /usr/www/users/tnevni/protection2.php on line 134

      I believe it's the header information being sent in the menu1.php script before the protection2 script is called where the cookie is created. I need a way to allow the header in menu1.php and still create the cookie.

      Thanks for help. Any additional thoughts?

      Gary

        If you are using a html based editor a spaces at the end of your script can cause this error.

        Try highlighting the end and deleting spaces.

        If you are not using an html based editor good luck cause I have no clue.

          Also, you absolutely cannot have anything before your first <?PHP tag.

            Well I removed all the echos from the first script

            Well, are you sure you have moved all output (even that from included files) to after the last header() or setcookie() ?

            /usr/www/users/tnevni/protection2.php on line 134

            What's on the new line 134?

            I believe it's the header information being sent in the menu1.php script before the protection2 script is called where the cookie is created. I need a way to allow the header in menu1.php and still create the cookie.

            You should be able to set a header and then set a cookie.

              That is correct; you can set a header, then a cookie, then a header if you like, that is unimportant. What is important is that no other information is outputted until you are done with your headers/cookies. Any echo, print, error message, anything that would cause php to send data the the client is a potential offender. Even a stray space or carriage return before the opening tag will cause this error.

                Thanks for all the help everyone. I figured it out with your help and it works great!

                I have one question concerning when the cookie expires. I currently have:

                setcookie('h4hcookie', $client_key, time()+606024*30);

                which causes the cookie to expire after 30 days. Is there a way to make the cookie permanent so it doesn't expire? Or should I just extend the time to several months?

                Thanks again for the help,

                Gary

                  Write a Reply...