if(!isset($_SESSION['valid_user']))
{ $username = $_POST["username"];
$password = $_POST["password"];
if($username && $password)
{ $query = "SELECT * FROM users WHERE username = '$username' and password = password('$password')";
$result = mysql_query($query);
if(!$result)
echo "Error running query";
$num_rows = mysql_num_rows($result);
if($num_rows > 0)
{ $_SESSION['valid_user'] = $username;
echo "Logged in as: <i><b>";
echo $_SESSION['valid_user'];
echo "</b></i>";
echo "<br><a href='login.php'>login</a>";
}
else
echo "not found";
}
else
echo "session variable not set";
}
else
{
echo "you are already logged in";
}
(<<<NOTE
if there was an error running the query, it will still continue on and try to use the $result.
if(!$result)
{ echo "Error running query";
$num_rows = 0;
}
else
$num_rows = mysql_num_rows($result);
NOTE;
)
"session variable not set"
Cutting out all the code that doesn't get run when that message comes up, and making empty() checks explicit:
if(!isset($_SESSION['valid_user']))
{ $username = $_POST["username"];
$password = $_POST["password"];
if(!empty($username) && !empty($password))
{
}
else
echo "session variable not set";
}
Swapping the if statement about to get rid of the no-op branch, and temporarily dealiasing the $_POST[] variables:
if(!isset($_SESSION['valid_user']))
if(!(!empty($_POST["username"]) && !empty($_POST["password"])))
echo "session variable not set";
Combining the if statements and doing a spot of boolean algebra to tidy up:
if(!isset($_SESSION['valid_user']) && (empty($_POST["username"]) || empty($_POST["password"])))
echo "session variable not set";
And there are the conditions required for the error message that comes up to come up. In la lengua inglesa, it reads:
The session variable must not be set AND one or both of the post variables must be empty.
Which sounds like the situation in which you want the message to appear.
We've narrowed the problem down. The fault is in one of those three variables. My money is on the session variable, but shall we be ceratin?
var_dump($_SESSION['valid_user'], $_POST['username'], $_POST['password']);
if(!isset($_SESSION['valid_user']))
...