cookie problems

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

User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

cookie problems

Post by cheatboy00 »

when i try to set 2 cookies i'm gettign this error

Warning: Cannot add header information - headers already sent by (output started at /home/dragonsb/public_html/cbaa/header.php:3) in /home/dragonsb/public_html/cbaa/l.php on line 13

Warning: Cannot add header information - headers already sent by (output started at /home/dragonsb/public_html/cbaa/header.php:3) in /home/dragonsb/public_html/cbaa/l.php on line 14

the code looks like this:

Code: Select all

setcookie ("idcbaa",$id,time()+604800,"/");
setcookie ("logcbaa",$stat,time()+604800,"/");
erg..
Elfstone
Forum Newbie
Posts: 21
Joined: Tue Jun 11, 2002 2:25 am
Location: NJ, USA

Post by Elfstone »

Check out This post. Should have the answer to your question.
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

but i cant move teh setcookie any futher up the page of code.......

Code: Select all

$qry = mysql_query("SELECT * FROM users WHERE login = '$login' AND pass = '$password'");

if (mysql_num_rows($qry) == 1){ 

   setcookie ("idcbaa",$id,time()+604800,"/");
   setcookie ("logcbaa",$stat,time()+604800,"/");
becasue i need the $qry before that to check to see if i should set the cookies or not....
Elfstone
Forum Newbie
Posts: 21
Joined: Tue Jun 11, 2002 2:25 am
Location: NJ, USA

Post by Elfstone »

The MySQL query wouldn't be the problem, is there any other code or HTML in that file?
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

Code: Select all

<?PHP

$dbh = mysql_connect ("localhost", "___", "___") or die ('I cannot connect to the database.');
   mysql_select_db ("dragonsb_xmb1");

$qry = mysql_query("SELECT * FROM users WHERE login = '$login' AND pass = '$password'");

if (mysql_num_rows($qry) == 1)&#123; 

   setcookie ("idcbaa",$id,time()+604800,"/");
   setcookie ("logcbaa",$stat,time()+604800,"/");

   $row = mysql_fetch_assoc($qry); 
   $stat = $row&#1111;'status'];
   $id = $row&#1111;'id'];
the code goes on after that... but could it be the fact, that i use this page as a sumbit result, i use a form to get login and pass then when you submit it code to this page
Elfstone
Forum Newbie
Posts: 21
Joined: Tue Jun 11, 2002 2:25 am
Location: NJ, USA

Post by Elfstone »

Are you including a header.php file before using the setcookie function? The code you pasted in your last post should be the very first thing in the file before any other code or HTML.
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

well.. i have3 a head.php but this file is included in it... its for logingin

so in header i have...

Code: Select all

echo("<table border=1  bordercolor=#444444 cellpadding="0" cellspacing="0" width="150"><tr>");
	   		echo("<td bgcolor="#444444" width="150" align="center">");
		   		echo("<font class="heading">Control Panel</font>");
			   echo("</td></tr>");
   			echo("<tr><td>");

         if (!$page OR $page != 'login')&#123;
            include ("log.php");
         &#125;

         else if ($page == 'login')&#123;
            include ("l.php");         

            if ($page == 'login' && $ucp == 'user' OR isset ($logcbaa) == 'user')&#123;
               include ("ucp.php");
            &#125;  

            if ($page == 'login' && $ucp == 'staffer')&#123;
               include ("scp.php");
            &#125;  

            if ($page == 'login' && $ucp == 'admin1')&#123;
               include ("acp.php");
            &#125;
         &#125;
theres the part where l.php (is thefile with the cookies).. theres more html etc.. above the code i just have more html etc.....
Elfstone
Forum Newbie
Posts: 21
Joined: Tue Jun 11, 2002 2:25 am
Location: NJ, USA

Post by Elfstone »

There is your problem, you sent HTML before setting the cookie, you can't do that. You need to set a cookie before ANY HTML tags have been sent, and before PHP has done anything to the page itself(echo'ing text to it, etc).
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

could it be possible at the top if the header.php to put somethign like this

Code: Select all

if ($check == 'y')&#123;
setcookie ......
&#125;
will that work, if i put that right at the top of the header.php page
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Yeah

Post by icesolid »

That code above is wrong. Try this.

Code: Select all

<?php
if($check == "y") &#123;
setcookie ...... 
&#125;
?>
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

im pretty sure that it doesn't matter
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

erg..... this is giving me a headace
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

oh... i got an idea can you add another page onto a form.... like the action would be one thing... there would be a hidden or somethign..... well just an idea
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: Yeah

Post by twigletmac »

icesolid wrote:That code above is wrong. Try this.

Code: Select all

<?php
if($check == "y") &#123;
setcookie ...... 
&#125;
?>
Just FYI -

Code: Select all

if ($check == 'y') &#123;
is the same thing as

Code: Select all

if ($check == "y") &#123;
Mac
LenHewitt
Forum Newbie
Posts: 10
Joined: Sat Jul 06, 2002 6:13 am
Location: United Kingdom

Post by LenHewitt »

Cheatboy,

If you include the line:

Code: Select all

ob_start("ob_gzhandler");
after your <?php tag, it will turn on output buffering for the page and may well solve your problem
Post Reply