I have a page that validates a user against a database. The first time in they enter a user id and a password in a form. The action of that form is to call the same page again.
At the top of the page, the script checks to see if a "$_POST" variable is set. If I do a write within the code (and therefore screw up the cookie writes and the redirect), it gets to that section of the code. If I don't do a write, the test for the post variable always fails. Here is the PHP code:
<?php
if ($POST[postaction] == "validateuser")
{
// page redisplayed with results of this code
$user = "me";
$pwrd = "mypassword";
$cont = "localhost";
$name_db = "mokenabarber";
$name_table = "UserID";
// form variables
$id = $POST["id"];
$pwd = $_POST["pwd"];
// set SQL session
$name_connect = mysql_connect($cont, $user, $pwrd) or die(mysql_error());
$db_connect = mysql_select_db($name_db, $name_connect) or die(mysql_error());
// set SQL call
$sql_code = "SELECT Password, Level FROM $name_table ";
$sql_code .= "WHERE ID = '$id'";
// testing only
// echo $sql_code . "<br><br>";
// check SQL output
$sql_result = mysql_query($sql_code,$name_connect) or die(mysql_error());
$output = mysql_fetch_array($sql_result);
// store database variables
$pwdSQL = $output["Password"];
$lvlSQL = $output["Level"];
// testing only
// echo "sql pwd: '" . $pwdSQL . "'<br>sql lvl: '" . $lvlSQL . "'<br>rows: '" . mysql_num_rows($sql_result) . "'<br>post pwd: '" . $pwd . "'<br>post id: '" . $id . "'<br>";
// set cookie time limit - 180 days
$hold = 60 60 24 * 180;
if ($pwdSQL == $pwd)
{
// validated user - write cookie and continue
setcookie("user", "$id", $hold);
setcookie("level", "$lvlSQL", $hold);
setcookie("authorized", "yes", $hold);
header("Location: http://www.mokenahickorycreek.com/SQL_Staff.php");
exit;
}
else
{
setcookie("user", "guest", $hold);
setcookie("level", "0", $hold);
setcookie("authorized", "no", $hold);
header("Location: http://www.mokenahickorycreek.com/index.php");
exit;
}
}
?>
The goofy lack of whitespace is due to the "manual" saying you can't have any prior to a cookie set. Of course, their example has it, but as it isn't working for me, I'll do it anyway they say.
TIA, LeeD