[英]Login.php does not redirect
我一直在尝试解决为什么我无法在登录后让这个 php 代码从 index.php 重定向到 profile.php。
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");
}
我已经研究过它并尝试了不同的方法和不同的网址,但到目前为止都没有成功。
任何帮助都会很棒!
header() 函数只有在执行前有 0 个输出时才会工作。 任何打印到浏览器的内容都会导致“header has been sent”,警告。你可以做的是使用元刷新标签,甚至是 JavaScript 重定向
<meta http-equiv="refresh" content="5; url=http://example.com/">
5 是重定向生效前的秒数。 如果您希望它是即时的,请输入 0。
或者你可以在 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";
为了使您的 PHP header() 函数工作,请确保之前没有输出任何内容。 您可以打开 PHP 错误以查看是否还有其他原因阻止了代码执行。
没有关于究竟发生了什么的更多信息,也没有提供您收到的消息错误,这是我可以给您的最佳答案。 请发布更多信息,我会很乐意编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.