setcookie error

hello

i use the fallowing code for my login page but it gives me the fallowing error i was wondering what is the problem? (line 179 is the setcookie)
by the way it doesn’t set the cookie
any help would be appreciated :slight_smile:
error:

Warning: Cannot modify header information - headers already sent by (output started at /home2/sportsh9/public_html/test/login_acc.php:6) in /home2/sportsh9/public_html/test/login_acc.php on line 179

my code:
[php] <?
include_once(‘functions.php’);

function cleanQuery($string)
{
if(get_magic_quotes_gpc()) // prevents duplicate backslashes
{
$string = stripslashes($string);
}
$badWords = “(delete)|(update)|(union)|(insert)|(drop)|(http)|(–)”;
$string = eregi_replace($badWords, “”, $string);
if (phpversion() >= ‘4.3.0’)
{
$string = mysql_real_escape_string($string);
}
else
{
$string = mysql_escape_string($string);
}
return $string;
}

if (isset($_COOKIE[‘scmuser’])) {
echo “<META HTTP-EQUIV=“Refresh” CONTENT=“1; URL=index.php”>”;
}else{
if ($_POST[‘username’]) {
//did they supply a password and username
$username=cleanQuery($_POST[‘username’]);
$password=cleanQuery($_POST[‘password’]);
if ($password==NULL || $username==NULL) {
?>

Username or password wasn't supplied!

Username: 
Password: 
Register - Forgot Your Password?
<? }else{ $query = mysql_query("SELECT username,password FROM users WHERE username = '$username'") or die(mysql_error()); $data = mysql_fetch_array($query); if($data['password'] != $password) { ?>

The supplied login was incorrect

Username: 
Password: 
Register - Forgot Your Password?
<? }else{ $query = mysql_query("SELECT username,password FROM users WHERE username = '$username'") or die(mysql_error()); $row = mysql_fetch_array($query); setcookie("scmuser", "$username", time()+3600); echo ""; } } } } ?>[/php]

thank you :slight_smile:

You need to eliminate any output to browser before sending headers (i.e. setcookie). Any single char, even space before opening php tag <?php will cause this error.

thank you for your answer but what should i do exactly? :frowning:
i don’t understand you :((

thank u again :slight_smile:

Yep phphelp is 100% correct!

You CANNOT output ANY data to the browser before using COOKIE, HEADER.

You can try to put ob_start(); just before any data is output and then ob_end_flush(); just after the setcookie command! This usually works for me! Also when setting a cookie use the path parameter,

e.g

setcookie(“scmuser”, “$username”, time()+3600, “/”);

Sponsor our Newsletter | Privacy Policy | Terms of Service