php - Why doesn't the redirect work? How would you fix it?

Solution:

You wouldn't use a header for this, try echoing the meta redirect tag.

if ($stmtselect->rowCount() > 0) {
    echo '<meta http-equiv = "refresh" content = "5; url = https://www.example.com" />';
    $_SESSION['accounts'] = $user;
    echo 'You have signed in successfully!';
    die();
}else {
    echo '<meta http-equiv = "refresh" content = "5; url = https://www.example.com/otherpage" />';
    $_SESSION['accounts'] = $user;
    echo 'Incorrect Username or Password or Email';
    die();
}

Answer

Solution:

It doesn't make a lot of sense to use AJAX if you're just going to redirect the user to another page immediately after it's finished. Here's a more conventional login procedure based on your code, which doesn't use AJAX at all.

Summary of changes:

  • make the form button type="submit"
  • make the form do a POST
  • remove all JavaScript code
  • delete jsLogin.php, and move relevant code from it to login.php and wrap inside an if which checks if the form has been submitted before trying to process it.
  • output an error message if login fails. If it succeeds just set the session variables and redirect to index.php

login.php:

<?php 
error_reporting(-1);
session_start();
    
if(isset($_SESSION['id16365171_hello_world_accounts'])){
    header("Location: index.php");
    die();
}

require_once('loginconfig.php');

//process the form submission, if any
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $email = $_POST['email'];
  $username = $_POST['username'];
  $password = $_POST['password'];
  $message = "";

  if (isset($email) && isset($username) && isset($password)) {
  
    $sql = "SELECT * FROM users WHERE username=? and password=? and email=? LIMIT 1";
    $stmtselect = $db->prepare($sql);
    $result = $stmtselect->execute([$username, $password, $email]);
    $user = $stmtselect->fetch(PDO::FETCH_ASSOC);

    if ($user) {
      $_SESSION['id16365171_hello_world_accounts'] = $user;
      header("Location: index.php");
      die();
    }
    else
    {
      $message = "Incorrect Username or Password or Email";
    }
  }
  else
  {
    $message = "Please enter all required details";
  }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="google-site-verification" content="0J0VoOQKJVdlFn7Us8_s97YvAXLirkBVrJ75FGLe_Ds" />
    <title>Hello World | Login</title>
    <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div class="container h-100">
    <div class="d-flex justify-content-center h-100">
        <div class="user_card">
            <div class="d-flex justify-content-center">
                <div class="brand_logo_container">
                    <img src="img/logo.png" class="brand_logo" alt="Programming Knowledge logo">
                </div>
            </div>  
            <div class="d-flex justify-content-center form_container">
                <?php if ($message != "") echo $message; ?> <!-- you might want to add some more HTML round that to make it display nicely -->
                <form method="POST">
                    <div class="input-group mb-2">
                        <div class="input-group-append">
                            <span class="input-group-text"><em class="fas fa-user"></em></span>                 
                        </div>
                        <input type="text" name="username" id="username" class="form-control input_user" placeholder="Username" required>
                    </div>
                    <div class="input-group mb-2">
                        <div class="input-group-append">
                            <span class="input-group-text"><em class="fas fa-key"></em></span>                  
                        </div>
                        <input type="password" name="password" id="password" class="form-control input_pass" placeholder="Password" required>
                    </div>
                    <div class="input-group mb-1">
                        <div class="input-group-append">
                            <span class="input-group-text"><em class="fas fa-inbox"></em></span>                    
                        </div>
                        <input type="email" name="email" id="email" class="form-control input_pass" placeholder="Email" required>
                    </div>
                    <div class="form-group">
                        <div class="custom-control custom-checkbox">
                            <input type="checkbox" name="rememberme" class="custom-control-input" id="customControlInline">
                            <label class="custom-control-label" for="customControlInline">Remember me</label>
                        </div>
                    </div>
                
            </div>
            <div class="d-flex justify-content-center mt-1 login_container">
                <button type="submit" name="button" id="login" class="btn login_btn">Login</button> 
            </div>
            </form>
            <div class="mt-3 mb-1">
                <div class="d-flex justify-content-center links">
                    Don't have an account? <a href="temp/home.php" class="ml-2">Sign Up</a>
                </div>
                <div class="d-flex justify-content-center">
                    <a href="#">Forgot your password?</a>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

P.S. Please also take note of the various security points I made in the comments above.

Answer

Solution:

Your javascript says

{-code-1}

Which means that if you echo out 1 then do something.

You should either change the php to echo out 1 or change the javascript to

if($.trim(data) === "You have successfully signed in!"){

The the redirect would work.

One more thing- Your redirect would be better if you did

setTimeout(function(){ document.location.replace("http://www.example.com"); }, 1000)

Source