PDA

View Full Version : A warning about Cookies


HSN
Tue 12th Aug '03, 12:45pm
Hello,

I'm developing a script in PHP and when I use a setcookie function I see the following warning:

Warning: Cannot add header information - headers already sent by (output started at C:\apache\htdocs\chapter_6_training\test.php:3) in C:\apache\htdocs\chapter_6_training\test.php on line 8


I used:

setcookie ('mycookie',$myname);

of even if I use a header(), it gives me the same warning!

like header ("Location: ........");

----
Please help me!

ffaBen
Tue 12th Aug '03, 1:29pm
turn on output buffering.
i.e. put
ob_start();
at the beginning of your script

HSN
Tue 12th Aug '03, 4:30pm
Nothing changed!

Still:

Warning: Cannot modify header information - headers already sent by (output started at.....

qxh
Tue 12th Aug '03, 4:47pm
Make sure you don't have an empty line or a space before <?php right at the top of the script, if that doesn't work post the first 10 lines of the script (if it's long) else all of it.

HSN
Tue 12th Aug '03, 5:07pm
if ($un && $ps )
{
require_once ('mysql_connect.php');

$query = " SELECT user_id FROM users WHERE (username = '$un' AND password = PASSWORD ('$ps') ) ";
$result = @mysql_query ($query);

$num = mysql_num_rows($result);

if ( $num )
{
setcookie ('first_name', $row[1] );
setcookie ('user_id', $row[0]);
echo "ok!";
exit ();
}
else
{
$message = '<p>Your username and password do not match our records.</p>';
}
mysql_close();
}



This is just a part of my incomplete script. In this if statement I used a setcookie.
I also removed the empty lines before <?php but...
this time it's not giving my warnings anymore, it does not display the login.php page!!

qxh
Tue 12th Aug '03, 5:11pm
Do you have MSN or AIM? If so, send me a PM with your info and I'll try and help you.

qxh
Tue 12th Aug '03, 5:29pm
All sorted :)

HSN
Tue 12th Aug '03, 5:58pm
Yup ;)