[thelist] PHP header wierdness

Morgan Kelsey morgan at morgankelsey.com
Tue Jan 21 12:17:01 CST 2003


tom,
> --------------------------------------------------
>
> The problem is, when I entered wrong info - even though
> mysql_num_rows($result) returned 0 as it should, the script still ran
> the LAST header, sending them to page.php instead. This is very odd,
> how can it jump out of an if loop with a header?
>
> Anyway, I added die(); after my first header and now it works..why?
> That seems very odd.
>

actually it's not odd.
until you add die() the HTML stream isn't stopped. so what is happening, is both
headers are being sent, and the second one is being executed.

it might make more sense to structure your code like this:

if(mysql_num_rows($result) == 1)
{
// Get db results
$row = mysql_fetch_array($result);

$aid   = $row["aid"];
setcookie("aid", $aid, "", "/");

header ("Location: page.php");
}
else
{
  $message = "We're sorry, either the user name or password you
entered was incorrect. Please try again.";
  header ("Location: login.php?msg=" . $message);
}

i think testing for a result set of one is better than testing for zero. using an
else also makes it easier to read, and spells out the intent of the code more
clearly. you may want to add another if for > 1, but that resultset will never
happen, right?
;-)


nagrom
http://www.morgankelsey.com




More information about the thelist mailing list