Cookies giving an error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
elecktricity
Forum Contributor
Posts: 128
Joined: Sun Sep 25, 2005 8:57 pm
Location: Trapped in my own little world.
Contact:

Cookies giving an error

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

roughian
Forum Newbie
Posts: 2
Joined: Sat Oct 29, 2005 5:00 pm

Post by roughian »

You can't send headers after you have sent data. Look at line 5 - you are outputting something there.
User avatar
elecktricity
Forum Contributor
Posts: 128
Joined: Sun Sep 25, 2005 8:57 pm
Location: Trapped in my own little world.
Contact:

Post 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');
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the HTML is output.... :?
User avatar
elecktricity
Forum Contributor
Posts: 128
Joined: Sun Sep 25, 2005 8:57 pm
Location: Trapped in my own little world.
Contact:

Post by elecktricity »

oh so your saying it has to be at the top? guess that would make sence... lemme try it
User avatar
elecktricity
Forum Contributor
Posts: 128
Joined: Sun Sep 25, 2005 8:57 pm
Location: Trapped in my own little world.
Contact:

Post by elecktricity »

it seems to be working great now... can somebody care to explain why you cant have anything before it though?
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post 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.
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post 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
Post Reply