嗯,我正在尝试实现 Steam OpenID 登录到网站,但我不太确定它是如何完成的以及 Steam 如何验证使用 OpenID 登录的用户。
至于现在我发现的是,steam 只返回用户 ID,没有返回任何其他内容,因此对于其余的事情,我必须使用 API 来获取用户的其他信息。
但我不太确定一旦有人通过 OpenID 登录,用户如何在网站上进行验证。
用户从 OpenID 登录后,我是否需要创建会话或设置 cookie 或将用户存储到数据库中?
try {
# Change 'localhost' to your domain name.
$openid = new LightOpenID('http://localhost/openid');
if(!$openid->mode) {
if(isset($_GET['login'])) {
$openid->identity = 'http://steamcommunity.com/openid';
header('Location: ' . $openid->authUrl());
}
echo '<li><a href="?login"><img border="0" src="http://cdn.steamcommunity.com/public/images/signinthroughsteam/sits_small.png" /></a></li>';
}
elseif($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
}
else {
$_SESSION['loged']=1;
header('Location: http://localhost/openid');
}
if(isset($_SESSION['loged'])) {
echo '<li><a href="?logout">Logout</a></li>';
}
if(isset($_GET['logout'])) {
unset($_SESSION['loged']);
}
echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
}
catch(ErrorException $e) {
echo $e->getMessage();
}
我以这段代码为例
我猜想
if(!openid->mode)
意思是如果 openid 没有设置?如果我按下该按钮,我应该显示登录按钮并转到 openid 提供商进行登录
接下来是如果用户不登录则显示取消消息
或者下一部分是如果用户登录,那么由于 openid 只返回用户 id,我需要以某种方式处理他并让他登录在我的网站上,对于这一部分,我应该设置一些会话或 cookie,我确实设置了会话并重定向用户返回主页。
但我不明白一些事情。
为什么我的登录按钮一直显示?
And this
echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
为什么它不起作用?总是显示用户未登录
这是我用来通过 Steam 的 OpenID 进行身份验证的代码
<?php
require 'includes/lightopenid/openid.php';
$_STEAMAPI = "YOURSTEAMAPIKEY";
// CHECK IF COOKIE EXISTS WITH PROFILE ID. IF NOT, LOG THE USER IN
try
{
$openid = new LightOpenID('http://URL.TO.REDIRECT.TO.AFTER.LOGIN/');
if(!$openid->mode)
{
if(isset($_GET['login']))
{
$openid->identity = 'http://steamcommunity.com/openid/?l=english'; // This is forcing english because it has a weird habit of selecting a random language otherwise
header('Location: ' . $openid->authUrl());
}
?>
<form action="?login" method="post">
<input type="image" src="http://cdn.steamcommunity.com/public/images/signinthroughsteam/sits_small.png">
</form>
<?php
}
elseif($openid->mode == 'cancel')
{
echo 'User has canceled authentication!';
}
else
{
if($openid->validate())
{
$id = $openid->identity;
// identity is something like: http://steamcommunity.com/openid/id/76561197960435530
// we only care about the unique account ID at the end of the URL.
$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
preg_match($ptn, $id, $matches);
echo "User is logged in (steamID: $matches[1])\n";
// HERE YOU CAN SET A COOKIE, SAVE TO A DATABASE, CREATE A SESSION, ETC.
// This is an example of what you can do once you have the profile id
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$_STEAMAPI&steamids=$matches[1]";
$json_object= file_get_contents($url);
$json_decoded = json_decode($json_object);
foreach ($json_decoded->response->players as $player)
{
echo "
<br/>Player ID: $player->steamid
<br/>Player Name: $player->personaname
<br/>Profile URL: $player->profileurl
<br/>SmallAvatar: <img src='$player->avatar'/>
<br/>MediumAvatar: <img src='$player->avatarmedium'/>
<br/>LargeAvatar: <img src='$player->avatarfull'/>
";
}
}
else
{
echo "User is not logged in.\n";
}
}
}
catch(ErrorException $e)
{
echo $e->getMessage();
}
?>
这将为用户提供一个 Steam 登录 ID 按钮,单击该按钮会将用户重定向到 Steam 社区登录页面。登录后,用户将被发送回您的域。这是 LightOpenID 构造函数中设置的内容。如果用户已通过验证,它将从返回值中提取唯一的玩家 ID。返回值看起来像http://steamcommunity.com/openid/id/76561194350435530
,你只需要76561194350435530
部分。使用它,您可以查询任何采用配置文件 ID 的 Valve API。
设置 cookie 和会话可以在登录过程结束时完成。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)