简体   繁体   中英

Login.php does not redirect

I have been trying to workout why I can not get this php code to redirect from index.php to profile.php after login.

The login.php works as I can go to profile.php manually and it would be logged in.

if ( password_verify($_POST['password'], $user['password']) ) {

    $_SESSION['email'] = $user['email'];
    $_SESSION['first_name'] = $user['first_name'];
    $_SESSION['last_name'] = $user['last_name'];
    $_SESSION['active'] = $user['active'];

    // This is how we'll know the user is logged in
    $_SESSION['logged_in'] = true;

    header("location: profile.php");
} else {
    $_SESSION['message'] = "You have entered wrong password, try again!";
    header("location: error.php");
}

I have looked into it and have tried different ways and different urls but thus far without success.

Any help would be great!

The header() function will only work if there's 0 output before its execution. Anything printed to the browser will cause a "header has already been sent”, warning. What you can do is use a meta refresh tag or even a JavaScript redirection

<meta http-equiv="refresh" content="5; url=http://example.com/">

5 is the number of seconds before the redirection takes effect. Put 0 if you want it to be instant.

Or you can do it in JavaScript like this:

// similar behaviour as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behaviour as clicking on a link
window.location.href = "http://stackoverflow.com";

In order to make your PHP header() function work, Make sure nothing is output before. You can turn on PHP errors to see if there's something else preventing the code execution.

Without any more info on what happens exactly or without providing the message error you are getting, this is the best answer I can give you. Please post more info and I will gladly edit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM