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
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Error :(

Post 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 :)
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post 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]
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post 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!
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

What is the output buffering?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

buyukcer wrote:What is the output buffering?
Check out http://www.php.net/manual/en/ref.outcontrol.php

Mac
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

thanks to twigletmac, i think i should observe the whole php.net site more carefully.
Post Reply