I have a simple login-form with username and password. The PHP after this form is this
<?php
session_start();
include 'global.php';
// Grab User submitted information
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query("SELECT username, password FROM users WHERE username = '$username' and password = '$password'");
$row = mysql_fetch_array($result);
if($row["username"]==$username && $row["password"]==$password)
{
echo 'ok';
}
else {
echo 'not ok';
}
?>
After submitting, the form shows the correct message (ok or not ok) if the username and password exists, but I can't seem to do more than echoing.
When the user exists, the form has to redirect to the index.php-page. I tried to put this in the if-clause
header('Location: index.php');
but that doesn't work :(
When the user doesn't exist, it has to show a message on the login form. So I put this in the else-clause
$_SESSION['errors'] = array("Your username or password was incorrect.");
header("Location: login.php");
This stores the message in the session, so when I put the following code on the login form
<?php if (isset($_SESSION['errors'])): ?>
<div class="form-errors">
<?php foreach($_SESSION['errors'] as $error): ?>
<p><?php echo $error ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
I hoped this would work too, but nothing does, sadly. Can someone assist me?