简体   繁体   English

Login.php 不重定向

[英]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.我一直在尝试解决为什么我无法在登录后让这个 php 代码从 index.php 重定向到 profile.php。

The login.php works as I can go to profile.php manually and it would be logged in. login.php 可以工作,因为我可以手动转到 profile.php 并且它将被登录。

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. header() 函数只有在执行前有 0 个输出时才会工作。 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任何打印到浏览器的内容都会导致“header has been sent”,警告。你可以做的是使用元刷新标签,甚至是 JavaScript 重定向

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

5 is the number of seconds before the redirection takes effect. 5 是重定向生效前的秒数。 Put 0 if you want it to be instant.如果您希望它是即时的,请输入 0。

Or you can do it in JavaScript like this:或者你可以在 JavaScript 中这样做:

// 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.为了使您的 PHP header() 函数工作,请确保之前没有输出任何内容。 You can turn on PHP errors to see if there's something else preventing the code execution.您可以打开 PHP 错误以查看是否还有其他原因阻止了代码执行。

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.请发布更多信息,我会很乐意编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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