Whenever verifyUser is returned true then the if statement should execute but for some reason instead of going to the header location, the page just refreshes and that’s all that happens. I’ve checked to be sure that the input information is correct and it is and when the information is incorrect the else statement executes perfectly fine. If anyone has any ideas as to why this is happening, please let me know. Thank you.
Here is the segment where the header() statement is made:
function validateUser($name, $pass)
{
$check = verifyUser($name, md5($pass));
if($check)
{
$_SESSION['status'] = 'authorized';
header('location: index.php');
} else{
echo'Please enter a correct username and password <br />';
echo "<a href='http://localhost/cms/admin/login.php'>Try Again?</a>";
exit;
}
}
Here is the verifyUser function just in case anyone needs it.
function verifyUser($name, $pass)
{
// Escape strings
$username = mysql_real_escape_string($name);
$password = mysql_real_escape_string($pass);
$result = mysql_query("select * from users where username='$username' and password='$password' limit 1");
if (mysql_num_rows($result)>0)
{
return true;
} else{
return false;
}
}
The most common reason that
header()doesn’t work is because something else has been output first.header()can only be called before anything else has sent output to the browser.If this is the case, PHP will throw an error when
header()is called. If you’re not displaying errors on the page, you can check your error logs to see if this is happening.You should also call
die()orexit()immediately after (or a soon as possible after) theheader()call, to prevent anything else from happening after the redirect header. It’s unlikely but possible that something later in the program could also cause the redirect to fail even where the initialheader()call succeeded.