T10*_*000 10 php openid lightopenid
您好
我几个小时前已经下载了LightOpenID(http://gitorious.org/lightopenid),但仍然无法弄清楚如何使它工作.
我把这个google示例保存在test.php文件中
<?php
require '../lib/init.php';
require '../lib/openID/openid.php';
try {
if(!isset($_GET['openid_mode'])) {
if(isset($_GET['login'])) {
$openid = new LightOpenID;
$openid->identity = 'https://www.google.com/accounts/o8/id';
header('Location: ' . $openid->authUrl());
}
?>
<form action="?login" method="post">
<button>Login with Google</button>
</form>
<?php
} elseif($_GET['openid_mode'] == 'cancel') {
echo 'User has canceled authentication!';
} else {
$openid = new LightOpenID;
echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
echo '<pre>'.print_r($openid,true).'</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
其中init.php是我的页面的init文件(常量,类,函数,数据库连接等).
运行此代码后,我按下了标有"Login with Google"的按钮,然后按下它
echo '<pre>'.print_r($openid,true).'</pre>';
Run Code Online (Sandbox Code Playgroud)
提供有关$ openid对象的一些信息
LightOpenID对象([returnUrl] => http://kur.com/openid.php [required] => Array()
Run Code Online (Sandbox Code Playgroud)[optional] => Array ( ) [identity:LightOpenID:private] => https://www.google.com/accounts/o8/id [claimed_id:LightOpenID:private] => https://www.google.com/accounts/o8/id [server:protected] => https://www.google.com/accounts/o8/ud [version:protected] => 2 [trustRoot:protected] => http://kur.com [aliases:protected] => [identifier_select:protected] => 1 [ax:protected] => 1 [sreg:protected] => [data:protected] => Array ( [login] => ))
......没什么特别......那就是它......
我花了很多时间在谷歌搜索教程,但连一个都找不到.你能帮我么.
如何登录用户?
从哪里我必须得到记录的用户信息(作为用户名,邮件)?
我从来没有使用过开放式ID而且我很困惑....
提前谢谢
Mew*_*ewp 34
在您的示例中,有一行显示如何完成身份验证:
echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
Run Code Online (Sandbox Code Playgroud)
如果$openid->validate()返回true,则表示声明的用户已通过$openid->identity身份验证.
如果您将其与标准身份验证进行比较:
$openid->validate()validate()返回true,则对用户进行身份验证(with $openid->identity),因此我们设置一个cookie来记住他(或者在成功登录时要做的其他事情).基本上,一旦你确认用户是他声称的那个用户(即他已经过身份验证),你就好像是一个正常的身份验证.
通常,您必须在某处存储身份以及会话ID.
用户名是$openid->identity.但是,您可能希望使用昵称作为显示的名称.但是,获取昵称和电子邮件地址需要进行其他配置.基本上,在打电话之前$openid->authUrl(),你必须添加:
$openid->required = array('namePerson/friendly', 'contact/email');
Run Code Online (Sandbox Code Playgroud)
该行将导致LightOpenID请求这些参数.您可以在axschema.org上查看其他参数列表(OP可能支持也可能不支持).然后,在调用之后validate(),获取它们的值,然后调用$openid->getAttributes().它将返回所有可用的参数,例如:
array(
[namePerson/friendly] => Mewp
[contact/email] => mewp@example.com
)
Run Code Online (Sandbox Code Playgroud)
但请注意,此列表可以包含其他参数,但可能不包含您请求的参数.基本上,OP可以自由地返回它想要的任何内容,因此您需要为缺少某些值做好准备.
小智 5
当用户点击'example-google.php页面上的'使用Google登录'按钮时,您将被重定向到谷歌,如果用户接受请求,他将再次被重定向到您的页面,您只能获得用户的Openid.
但是,如果您想获得其他信息或更改OpenID,您可以通过以下方式进行操作:
<?php
require 'openid.php';
try {
$openid = new LightOpenID;
if(!$openid->mode) {
if(isset($_GET['oidType'])) {
$oidType = $_GET['oidType'];
$openid = new LightOpenID;
if ($oidType==1)
{
$openid->identity = 'https://www.google.com/accounts/o8/id';
}
else
{
$openid->identity = 'https://me.yahoo.com ';
}
$openid->required = array(
'namePerson',
'namePerson/first',
'namePerson/last',
'contact/email',
);
$openid->returnUrl = 'http://www.yourdomain.com/login.php';
header('Location: ' . $openid->authUrl());
}
?>
<a href="?oidType=1">Login with Google</a>
<a href="?oidType=2">Login with Yahoo</a>
<?php
} elseif($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
}
} elseif($openid->validate()) {
$openid_identity = $openid->identity;
$data = $openid->getAttributes();
$email = $data['contact/email'];
$namePerson = $data['namePerson'];
$first = $data['namePerson/first'];
$last = $data['namePerson/last'];
echo "Openid:$identitystr <br>";
echo "Email : $email <br>";
echo "namePerson : $namePerson <br>";
echo "first : $first <br>";
echo "last : $last <br>";
} else {
echo "The user has not logged in";
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
这个脚本现在可以在我的本地主机上正常工作,在我的笔记本电脑上运行 apache,并通过 wifi 连接到互联网。
有人告诉我,您应该在创建新的 LightOpenId 对象时将您的域传递给它。
$iniConfig是一个 parse_ini_file 数组,存储在文档根目录之外,我在其中存储所有重要变量。
在这种情况下
[openid]
domain='mydomain.com'
Run Code Online (Sandbox Code Playgroud)
因此,我创建新对象并包含服务器所在的域:
$openid = new LightOpenID($iniConfig['openid']['domain']);
Run Code Online (Sandbox Code Playgroud)
我是这样写的,没有检查它是否可以在没有域的情况下工作。