1

i am trying to redirect the user when the login details are correct, but my header function is not running, I have tested the if statement without the header function by echo something out and this worked, so im not sure why the header is not being executed. any help would be appreciated.

login php

    <?php
session_start();
require '../includes/class/login.php';
$login = new login();
$username_error=' ';
$password_error=' ';
$result=' ';
if(isset($_POST['username']) && $_POST['username'] !== ""){
$username = $_POST['username'];
$username = trim($username);
$user_exists = $login->user_exists($username);
if ($user_exists) {
$username_error = 'correct username entered ';
} else {
$username_error = 'username not found';
  }
}

if(isset($_POST['password']) && $_POST['password'] !== ""){

$password = $_POST['password'];
$password = trim($password);
$password_check = $login->password_check($username,$password);
if ($password_check) {
$password_error = 'correct password entered ';
} else {
$password_error = 'password not found';
  }
}

if (isset($_POST['login'])&& $_POST['login'] !== "") { 
$log = $login->check_login($username,$password);

if ($log) {

 $login->redirect('home.php');

 //$user_session = 'welcome'.' '.$_SESSION['username'];

} else {
// Registration Failed
$password_error='Wrong username or password';
        }
    }

$return_data=array('username'=> $username_error, 'password'=> $password_error ,'user_session'=>$_SESSION['username']);

header('Content-Type: application/json');
echo json_encode($return_data);
exit();

?>

login in class

  public function redirect($url)
   {
       header("Location: $url");
   }
percy3872
  • 91
  • 1
  • 8
  • Possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Qirel Oct 28 '15 at 15:30

1 Answers1

1

You have some sort of output, which is causing "Headers already sent" error. You also have php errors disabled. Enable the errors and check it out (or check your apache error log).

Angel Iliikov
  • 710
  • 5
  • 8
  • 1
    After your edit, are you sure there is no empty space before the opening php tag ? If there is one - this is your problem. – Angel Iliikov Oct 28 '15 at 15:27
  • To expand on the answer, you can have **no** kind of HTML, whitespace, tab, PHP `echo` prior to using `header()`. You most likely have it, you just need to find it. Check out [How do I get PHP Errors to display?](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). And also possibly this duplicate: [How to fix “Headers already sent” error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Qirel Oct 28 '15 at 15:30
  • Also, from that last link, what is your encoding set to? It should be `UTF-8 without BOM` – Qirel Oct 28 '15 at 15:31
  • i have echo output before because i am using jquery to dynamically check the user input – percy3872 Oct 28 '15 at 15:32
  • You can not have echo or any other output before the redirect. You must rethink your script / application logic. – Angel Iliikov Oct 28 '15 at 15:40
  • @percy3872 It can't be `UTF-8`, it has to be `UTF-8 w/o BOM`. And you **can not** have any kind of output what-so-ever before using `header()`, that's just the way it is. If that's the case, you'll have to rebuild your application as Angel said. – Qirel Oct 28 '15 at 16:02