1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    It dosn't work is no accurate error description. Any PHP Error? Error Level set to show all? What happens in the Browser Console, when redirect should happen? – Christian Gollhardt Aug 24 '14 at 11:05
  • @Christian When I put the `header('Location: index.php');` in the if-clause, he just redirects to the (blank) action-page in my form. This action-page contains this code: `` – Jan Quintelier Aug 24 '14 at 15:37

2 Answers2

0

before header should not be any echo, var_dump, print_r or any html or any text and write exit() after header

header('Location: index.php');
exit();
baravak
  • 33
  • 1
  • 1
  • 5
0

if you have result

?>
<?php

header location not works in this case.

another way is using ob_start(); at the top

ob_start();
header("Location: yourpage.php");
exit();

Reference : header location not working in my php code

Community
  • 1
  • 1
Farshad
  • 1,465
  • 1
  • 9
  • 12
  • ob_start() at the top did the trick for the redirect, thank you! If the username doesn't exist, he stays on the login-page, but he doesn't seem to show the error. – Jan Quintelier Aug 24 '14 at 15:59
  • you can store error in session or local variable and show every where you need. other way is you can redirect with header location and set msg as a query string to new page `header("Location: redirect.php?msg=loginError");` then in redirect page show error from `$_GET['msg']` – Farshad Aug 24 '14 at 16:19
  • Thanks again! Your way also work, but I made it work like by putting this code in my login-form `` If I could I'd upvote, but I'm missing some rep to do that, sry :) – Jan Quintelier Aug 24 '14 at 16:30