...

View Full Version : "Headers already sent" Cookie Problem



binaryWeapon
03-02-2008, 10:43 PM
I'm getting the error that "headers were already sent" in my php prog for each time I try to call or even delete a cookie.



Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/testzone/newLoginTest.php:80) in /home/nathan/public_html/testzone/newLoginTest.php on line 30

Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/testzone/newLoginTest.php:80) in /home/nathan/public_html/testzone/newLoginTest.php on line 31

Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/testzone/newLoginTest.php:80) in /home/nathan/public_html/testzone/newLoginTest.php on line 32

Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/testzone/newLoginTest.php:80) in /home/nathan/public_html/testzone/newLoginTest.php on line 33

Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/testzone/newLoginTest.php:80) in /home/nathan/public_html/testzone/newLoginTest.php on line 89




function login($username, $password, $type, $hidden)
{
mysql_connect("localhost", "*********", "*********") or die("Error inputting information to the database. Please try again later, or email us at excalo@cox.net. Sorry!");
mysql_select_db("********") or die("Error connecting to the database. Please try again later, or email us at excalo@cox.net. Sorry!");

$unames=array();
$res = mysql_query("SELECT username FROM users");
while ($row = mysql_fetch_row($res)) {
array_push ($unames, $row[0]);
}

$passes=array();
$res2 = mysql_query("SELECT password FROM users");
while ($row = mysql_fetch_row($res2)) {
array_push ($passes, $row[0]);
}

$numU = count($unames);

for($i=0; $i<$numU; $i++)
{
if($unames[$i]==$username)
{
if($passes[$i]==$password)
{
if($type=="auto")
{
$inTwoMonths = 60 * 60 * 24 * 30 * 12 * 10 + time();
setcookie("loggedin", "true", $inTwoMonths);
setcookie("username", $unames[$i], $inTwoMonths);
setcookie("password", $passes[$i], $inTwoMonths);
setcookie('content',$hidden,1);
}
}
else if($password=="")
{
if($type=="auto")
{
setcookie("loggedin", "", time()-3600);
setcookie("username", "", time()-3600);
setcookie("password", "", time()-3600);
setcookie('content','Access denied!',0);
}
elseif($type=="manual")
{
echo "No password given!";
}
}
else if($passes[$i]!=$password)
{
if($type=="auto")
{
setcookie("loggedin", "", time()-3600);
setcookie("username", "", time()-3600);
setcookie("password", "", time()-3600);
setcookie('content','Access denied!',0);
}
elseif($type=="manual")
{
echo "Incorrect password!";
}
}
$uGood="true";
}
}

if(!$uGood && $username!="")
{
echo "Incorrect username!";
}
else if(!$ugood && $username=="")
{
echo "No username given!";
}
}

if ($_COOKIE['loggedin'] && $_COOKIE['username'] && $_COOKIE['password'])
{
echo "um, hi!";
login($_COOKIE['username'], $_COOKIE['password'], 'auto', 'YO!');
}
else
{
echo "um, bye.";
}

echo $_COOKIE['content'];
setcookie("content", "", time()-3600);


Any ideas?
Oh yeah, its the beggening ideas of a login system I'm designing, if that matters at all.

digitalfiz
03-02-2008, 10:47 PM
a space before your <?php tags or a space after your ?> tags in your initial includes can cause this problem. Your not alowed to output anything at all even whitespace before all your headers are sent and yes cookies and session_start are included in the headers.

Inigoesdr
03-02-2008, 11:09 PM
output started at /home/nathan/public_html/testzone/newLoginTest.php:80
What's on line 80?

binaryWeapon
03-02-2008, 11:39 PM
What's on line 80?

echo "um, hi!"; I was trying to start simple, to just echo something if all the cookies required for the login were set. That part worked. An echo shouldn't disallow cookies from being called, should it?

a space before your <?php tags or a space after your ?> tags in your initial includes can cause this problem. I checked, no whitespaces, no characters no nothing before or after the php tags (the whole document is just the block of php in the first post).

GO ILLINI
03-02-2008, 11:50 PM
yes the echo is causing the problem.
Nothing can be printed to the page before you use the session,cookie,or headers commands.
Remove that and all should be well.

-Adam

javanoobie9
03-03-2008, 12:07 AM
just put your header on top of the page before anything prints.

digitalfiz
03-03-2008, 12:36 AM
You can also look into output buffering or setting variables to echo at the end after your if statements are done. Outputting anything at any point ends your ability to set anymore headers because they are already sent to the browser.

binaryWeapon
03-05-2008, 12:06 AM
Alright, sorry I haven't posted for a couple days; I was busy. I got rid of the echo and *viola!* it worked! Thank you to everyone who responded on this post; I really appreciate it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum