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");
.
.
.