Page 1 of 1

Error :(

Posted: Sat Apr 27, 2002 10:30 am
by Jim
I'm learning how to set cookies right now, and I'm trying to set a cookie when the user logs in to my site. This is the code I'm using on the log in page:

Code: Select all

<?

setcookie("username",$name,time()+1209600);

include("/home/maxxxtor/public_html/prodigy/php/login/connect.php");


$sql = "SELECT id FROM pro_members WHERE name='$rname' and pass='$rpass'"; 

$result = mysql_query($sql) or die ("Unable to get results."); 

$num = mysql_numrows($result) or die ("Your Username and/or Password are incorrect.  If you feel you have recieved this message in error, please contact the <a href="mailto:website@hot.rr.com">webmaster</a>"); 

if ($num == 1) &#123; 

echo "Welcome to Prodigy!  You are logged in as $rname !";

&#125; 

?>
When I run it I get this error:

Warning: Cannot add header information - headers already sent by (output started at /home/maxxxtor/public_html/prodigy/php/login/login2.php:3) in /home/maxxxtor/public_html/prodigy/php/login/login2.php on line 5

I've tried figuring out what this meant, but have no idea. Help would be appreciated, and if there is a site somewhere that explains certain errors to you, please show me that, too :)

Posted: Sat Apr 27, 2002 10:40 am
by samscripts
Hi, from the php manual chapter on sending headers (applies to cookies to), at http://www.php.net/manual/en/function.header.php
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Basically, your script must not have any html (or just blank lines) before your cookie setting stuff or you will get this error.

Alternatively, you can use output buffering to get around this.

Hope this helps, Sam[/url]

Posted: Sat Apr 27, 2002 10:45 am
by Jim
Thanks a ton. It did indeed help. I got rid of the spaces and the cookie works just fine :) Now let's see if I can get information from it correctly...

Thanks again!

Posted: Sat Apr 27, 2002 11:42 am
by dethron
What is the output buffering?

Posted: Sat Apr 27, 2002 12:22 pm
by twigletmac
buyukcer wrote:What is the output buffering?
Check out http://www.php.net/manual/en/ref.outcontrol.php

Mac

Posted: Sat Apr 27, 2002 12:29 pm
by dethron
thanks to twigletmac, i think i should observe the whole php.net site more carefully.