Php Can';使用OpenID登录时是否传递会话变量?
我尝试运行这段代码,为Google电子邮件创建一个会话变量,但由于某种原因,Php Can';使用OpenID登录时是否传递会话变量?,php,session,authentication,google-openid,Php,Session,Authentication,Google Openid,我尝试运行这段代码,为Google电子邮件创建一个会话变量,但由于某种原因,$\u会话['email']不想存储数据。我运行代码,登录谷歌后得到的唯一输出是 hello world 1 hello world 2 <?php session_start(); require 'openid.php'; $openid = new LightOpenID('http://www.splunk.com'); if (!$openid->mode) { $openid
$\u会话['email']
不想存储数据。我运行代码,登录谷歌后得到的唯一输出是
hello world 1
hello world 2
<?php
session_start();
require 'openid.php';
$openid = new LightOpenID('http://www.splunk.com');
if (!$openid->mode)
{
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
header('Location: ' . $openid->authUrl());
}
else
{
if ($openid->validate())
{
//echo "Hello World!";
$attributes = $openid->getAttributes();
$google_email = $attributes['contact/email'];
$_SESSION['email'] = $google_email;
echo $google_email."<br />";
echo "<pre>" . print_r($_GET, true) . "</pre>";
echo $_GET['contact_email'];
header("Location: " . google_login.php);
}
else
{
echo '<p>hello world 1</p>';
echo "<p>". $_SESSION['email']. "</p>";
echo '<p>hello world 2</p>';
}
}
?>
根据您的代码,
$openid->validate()
返回false
,因此会话变量从未设置。我明白了,您能帮我找到解决该问题的方法吗?我不知道如何让$openid->validate()
返回true。你应该检查你正在使用的openid
脚本的文档,或者在这里搜索如何让openid工作/验证。我已经在这个网站上搜索过了,但运气不好,我就是从那里得到这个的。没有回答???我不知道为什么谷歌这么难做到这一点。
<?php
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
require 'openid.php';
try
{
# Change 'localhost' to your domain name.
$openid = new LightOpenID('http://www.splunk.com');
if(!$openid->mode)
{
if(isset($_GET['login']))
{
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
header('Location: ' . $openid->authUrl());
}
?>
<form action="?login" method="post">
<button>Login with Google</button>
</form>
<?php
}
else if($openid->mode == 'cancel')
{
echo 'User has canceled authentication!';
}
else
{
$attributes = $openid->getAttributes();
$google_email = $attributes['contact/email'];
echo $google_email;
echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
}
} catch(ErrorException $e) {
echo $e->getMessage();
}