我正在使用google sign-in,并且大部分时间都在那里。用户成功登录,我可以访问我的mysql用户表来查找用户记录。在完成用户处理并设置$_SESSION变量之后,我想从POST页面重定向回我的index.php页面。我知道POST页面正在执行,但重定向不起作用,我没有看到任何错误(比如“无法修改标题信息-标题已发送”。
login.php包含谷歌登录按钮,并调用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);
...
includes/oauth.php文件接受令牌并对其进行验证。获取userId,在我的数据库中查找并准备会话变量。在这一点上,它应该重定向,但没有。我被难住了。这是不是因为我在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();
}
...
转载请注明出处:http://www.shenkehuoyun.com/article/20230526/1750556.html