doumu6941 2013-10-02 13:17
浏览 565

如何添加Steam OpenID登录并记住用户登录cookie到我的网站?

Im trying to build a website where users will be able to login via Steam OpenID and do some stuff on the website.

But i would also like to implement some features that steam doesn't offer so:

If user come first time to the website and try to login he use steam OpenID feature to retrieve his user informations (the id is only passed on openID and the rest of informations i can take via API) but now i need to verify user first before i enable other site features, to verify user i need to check if he have more than 20 games on his account. If he have more than 20 games he is ok to continue to use website, if he don't have enough games post a notice that once he have enough game he can continue with login.

When the user is logedin and verified i need to insert his data into database to be able to connect him with features on my website, like counting comments, points,and other activity.

And finally once he is loged in remember him via cookie (there are sessions but as i know they are only valid until browser is closed) so cookie would be solution next time he come to website he don't need to login again.

I know how to do some stuff but in messy way i would be very grateful if someone could point me out and help me a bit how to achieve at least half of this.

So far the code i have is this one.

function login() {
$openid = new LightOpenID(SITEURL);

if(!$openid->mode) {

    if(isset($_GET['go']) && $_GET['go'] == 'login' ) {
        $openid->identity = 'http://steamcommunity.com/openid';
        header('Location: ' . $openid->authUrl());
    }

    if(!isset($_SESSION['RaffleSteamAuth'])) {
        echo '<li><a href="?go=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.';
}

elseif(!isset($_SESSION['RaffleSteamAuth'])) {

        $_SESSION['RaffleSteamAuth'] = $openid->validate() ? $openid->identity : null;
        $_SESSION['RaffleSteamID64'] = str_replace('http://steamcommunity.com/openid/id/', '',$_SESSION['RaffleSteamAuth']);
        if($_SESSION['RaffleSteamAuth'] != null) {
            $steam64 = str_replace('http://steamcommunity.com/openid/id/', '', $_SESSION['RaffleSteamAuth']);
            $profile = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . APIKEY . '&steamids=' . $steam64);
            $buffer = fopen('cache/' . $steam64 . '.json', 'w+');
            fwrite($buffer, $profile);
            fclose($buffer);
        }

        header('Location: ' . SITEURL);
    }

if(isset($_SESSION['RaffleSteamAuth'])) {
    $steam = json_decode(file_get_contents('cache/' . $_SESSION['RaffleSteamID64'] . '.json'));
    echo '<li class="dropdown">';
    echo '<a href="#" class="dropdown-toggle" data-toggle="dropdown"><img src="' . $steam->response->players[0]->avatar . '"/> ' . $steam->response->players[0]->personaname .'<b class="caret"></b></a>';
    echo '<ul class="dropdown-menu">';
    echo '<li><a href="?go=logout">Logout</a></li>';
    echo '</ul>';
    echo '</li>';

}

if(isset($_GET['go']) && $_GET['go'] == 'logout' ){
    unset($_SESSION['RaffleSteamAuth']);
    unset($_SESSION['RaffleSteamID64']);
    header('Location: ' . SITEURL);
}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!