Page 1 of 1

Cookies giving an error

Posted: Sat Oct 29, 2005 6:12 pm
by elecktricity
I have it to were when you update the profile and change the password it will just delete the cookie so you will login again with the new password and it's giving me an error and not expiring the cookie... here is the expire cookie code:

Code: Select all

<?PHP
setcookie ('user', '', time()-3600, '/dir/', '', 0);
setcookie ('pass', '', time()-3600, '/dir/', '', 0);
?>
which /dir/ is a valid directory and the one im using and this is the error it's giving me:
Warning: Cannot modify header information - headers already sent by (output started at /home/rootbee/public_html/site/dir/profile2.php:5) in /home/rootbee/public_html/site/dir/profile2.php on line 117

Warning: Cannot modify header information - headers already sent by (output started at home/rootbee/public_html/site/dir/profile2.php:5) in /home/rootbee/public_html/site/dir/profile2.php on line 118
line 117 and 118 is the 2 cookie lines... everything besides this does work though, I tried without that code.

Posted: Sat Oct 29, 2005 7:07 pm
by shiznatix

Posted: Sat Oct 29, 2005 7:17 pm
by roughian
You can't send headers after you have sent data. Look at line 5 - you are outputting something there.

Posted: Sat Oct 29, 2005 8:48 pm
by elecktricity
roughian wrote:You can't send headers after you have sent data. Look at line 5 - you are outputting something there.
there is nothing outputting on line 5... here are the first 6 lines:

Code: Select all

<html>
<head>
<title>Edit User Profile</title>
</head>
<?PHP
include ('header.php');

Posted: Sat Oct 29, 2005 8:56 pm
by feyd
the HTML is output.... :?

Posted: Sat Oct 29, 2005 9:00 pm
by elecktricity
oh so your saying it has to be at the top? guess that would make sence... lemme try it

Posted: Sat Oct 29, 2005 9:45 pm
by elecktricity
it seems to be working great now... can somebody care to explain why you cant have anything before it though?

Posted: Sat Oct 29, 2005 9:47 pm
by mickd
because cookies are sent with the headers so if the headers are already sent then it cant send the cookies

setcookie
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace. If output exists prior to calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie.
hope that helps.

Posted: Sat Oct 29, 2005 10:02 pm
by yum-jelly
There are times that setting a cookie can not be done at the beginning of ones script. If this happens have a look at * output_buffering *. It will allow you to do this and more, as it gives you complete control of the output buffer!

yj