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");
   }
1

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

6 Comments

After your edit, are you sure there is no empty space before the opening php tag ? If there is one - this is your problem.
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?. And also possibly this duplicate: How to fix “Headers already sent” error in PHP
Also, from that last link, what is your encoding set to? It should be UTF-8 without BOM
i have echo output before because i am using jquery to dynamically check the user input
You can not have echo or any other output before the redirect. You must rethink your script / application logic.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.