Hi!
I have read through the forums, but was unable to find an answer to my exact problem. I hope someone will help me. I am new to php and I got this code from a tutorial, but it is not working for me. It works when I remove the code for setting cookies, but a login without cookies is pointless. I keep getting the error messages:

Warning: Cannot modify header information - headers already sent by (output started at /home/forceofg/public_html/bookatopia.com/config.php:1) in /home/forceofg/public_html/bookatopia.com/login.php on line 17

Warning: Cannot modify header information - headers already sent by (output started at /home/forceofg/public_html/bookatopia.com/config.php:1) in /home/forceofg/public_html/bookatopia.com/login.php on line 18
You are now logged in!
Continue to the members section.

This is my code:

<?php
include("config.php");
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
$match = "select id from $table where username = '".$_POST['username']."'
and password = '".$_POST['password']."';"; 
$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry); 
if ($num_rows <= 0) { 
echo "Sorry, there is no username $username with the specified password.<br>";
echo "<a href=login.html>Try again</a>";
exit; 
} else {
setcookie("loggedin", "TRUE", time()+(3600 * 24));
setcookie("mysite_username", "$username");
echo "You are now logged in!<br>"; 
echo "Continue to the <a href=members.php>members</a> section.";
}
?>

Please help me. Thank you.

    The error stems from sending output to the browser (any output including white-space) and then trying to call a function that requires HTTP header modification (like [man]setcookie/man). From the code you posted it looks as though everything should work; however, check the beginning of your file for a space or even a Byte-order-mark in case your file was saved as UTF and you didn't mean for it to be.

      Hi!
      Thank you for your reply. I checked my code for spaces and could not find any. Also, I had saved my file as UTF-8 and the language on my server is also UTF-8 so they should be compatible, yet I am I'm still getting that terrible error message. I don't know how I can fix it.

        check that there are no line returns after the closing ?> in "config.php" - if you use an editor with line numbers you should be able to see if the numbering continues after the ?> or not - i had a problem with that once

          Write a Reply...