-1

I want it to redirect the user based on the if statements, but it doesn't seem to work. Any ideas?

<?php
$username = $_POST['username'];
$password = $_POST['password'];

if ($username == 'co323'){
if($password == 'marking'){
    $link = "menu.php";
 }
else{
    echo "Wrong Username/Password";
    $link = "loginform.html";
 }
}
else{
echo "Wrong Username/Password";
$link = "loginform.html";
}
header($link);

?>
  • Any errors on screen or in the logs? – GolezTrol Apr 05 '15 at 21:27
  • `"it doesn't seem to work"` - Is this just a feeling that you have? Or is there some actual *indication* that this "doesn't work"? Describe the problem. – David Apr 05 '15 at 21:28
  • It's a good habit to use exit() after header redirection because script doesn't stops with execution immediately after. This is not relevant for your script, but sometimes this can lead you into problems. – Whirlwind Apr 05 '15 at 21:40

1 Answers1

2

To redirect, you need a Location header, so the proper line would be:

header("Location: $link");

Note that you must not echo any output before the redirect header, so remove all your echos to make this work.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • 2
    He/she should also comment out the `echo`s. – chris85 Apr 05 '15 at 21:37
  • It's also a good idea to throw an `exit;` afterwards, to stop any further execution of the script (I realise it does appear to be the last line in this case anyway) – rjdown Apr 05 '15 at 21:38
  • @chris85 Good point. You must not have any output before the redirect. output after outputting the Location header is allowed, but useless since the browser will redirect immediately, so you can't see the actual message. – GolezTrol Apr 05 '15 at 21:39