Bizarre Headers Error when setting Cookies

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Bizarre Headers Error when setting Cookies

Post by simonmlewis »

[text]Warning: Cannot modify header information - headers already sent by (output started at /home/j562595/public_html/site/a_sitelogin.php:16) in /home/j56259544/public_html/site/a_sitelogin.php on line 17

Warning: Cannot modify header information - headers already sent by (output started at /home/j562595/public_html/site/a_sitelogin.php:16) in /home/j56259544/public_html/site/a_sitelogin.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at /home/j562595/public_html/sites/a_sitelogin.php:16) in /home/j56259544/public_html/site/a_sitelogin.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/j562595/public_html/site/a_sitelogin.php:16) in /home/j56259544/public_html/site/a_sitelogin.php on line 20

Warning: Cannot modify header information - headers already sent by (output started at /home/j562595/public_html/site/a_sitelogin.php:16) in /home/j56259544/public_html/site/a_sitelogin.php on line 21[/text]

We are now hosting about 5 sites on a new service. All are ok. We can login to them all fine - except this one.
The cost is the same, except this uses PDO.

For some reason, it won't let us set Cookies. If I echo the output of the database query, it shows the data, but just won't set the cookies.
I don't know what ..... .php:16 means either.

Why would all the sites work, but this one? The code is the same. Especially the SetCookie code.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Bizarre Headers Error when setting Cookies

Post by Celauran »

It's complaining about something on line 16 of a_sitelogin.php. What's going on there?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Bizarre Headers Error when setting Cookies

Post by simonmlewis »

Code: Select all

while ($row = mysql_fetch_object($result)) 
	{
	echo "<b>$row->email</b><br/><b>$row->type</b>";
setcookie("user", $row->firstname, time()+13600);
setcookie("email", $email, time()+13600);
setcookie("userid", $row->id, time()+13600);
setcookie("type", $row->type, time()+13600);
setcookie("username", $row->username, time()+13600);
Line 14 > line 21 shown above. The same as our other web sites.

It's not storing the cookies.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Bizarre Headers Error when setting Cookies

Post by Celauran »

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.
setcookie()
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Bizarre Headers Error when setting Cookies

Post by simonmlewis »

Probably... but then ALL (and I mean ALL) our web sites that use Cookies, run correctly without a hitch, like this.
So why is this one web site doing this.
We now have three domains on this server, and they all have idential scripts.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Bizarre Headers Error when setting Cookies

Post by simonmlewis »

Sorted it.
I had echoed something before it, but only to test. Now I have removed it - it works. Yet it wasn't there before and didn't work! lol
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply