我正在使用谷歌登錄,并且大部分時間都在那里。用戶成功登錄,我可以訪問我的 mysql 用戶表來查找用戶記錄。在完成用戶處理并設定 $_SESSION 變數后,我想從 POST 頁面重定向回我的 index.php 頁面。我知道 POST 頁面正在執行,但重定向不起作用,我沒有看到任何錯誤(例如“無法修改標頭資訊 - 標頭已發送”。
login.php 包含 google 登錄按鈕并在 includes/oauth.js 中呼叫一個 js 函式
...
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script src="includes/oauth.js"></script>
</head>
<body>
<div id="content">
<div class="g-signin2" data-longtitle="true" data-onsuccess="onSignIn"></div>
...
onSignIn 函式負責登錄程序并檢索用戶詳細資訊。它還準備 POST 呼叫,包括身份驗證令牌
...
var xhr = new XMLHttpRequest();
xhr.open('POST', 'includes/oauth.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send('idtoken=' id_token);
...
include/oauth.php 檔案獲取令牌并對其進行驗證。獲取用戶 ID,在我的資料庫中查找并準備會話變數。在這一點上,它應該重定向,但沒有。我難住了。這是因為我在 POST 中使用了 XMLHttpRequest() 嗎?
<?php
session_start();
...
if (isset($_POST['idtoken'])){
...
$_SESSION["auth"] = true;
$_SESSION["userId"] = $row['id'];
$_SESSION["userName"] = $row['name'];
header("Location: ../index.php");
exit();
}
...
uj5u.com熱心網友回復:
完美的!謝謝,@Barmar。
我更新了includes/oauth.js檔案如下”
var xhr = new XMLHttpRequest();
xhr.open('POST', 'includes/oauth.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
window.location = "../index.php";
xhr.send('idtoken=' id_token);
現在一切正常!
uj5u.com熱心網友回復:
(我沒有足夠的代表發表評論,所以如果我能弄清楚,我會編輯這個答案)
但重定向不起作用,我沒有看到任何錯誤(例如“無法修改標頭資訊 - 標頭已發送”。
那么最終會發生什么?
也許,頁面是否繼續加載/處理并且沒有結束?我正在研究一個類似的 Cookie/post 相關問題,它可能是一個類似的解決方案。就我而言,我必須使用,cookie_write_close()因為我會設定取決于當前session_start()背景關系的cookie 。在添加之前cookie_write_close(),php 行程將保持打開狀態,并且不會發生重定向加載。相反,頁面將根據服務器超時設定超時。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/318493.html
