[英]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.