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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
    • ¥15 求解 yolo算法问题
    • ¥15 虚拟机打包apk出现错误
    • ¥30 最小化遗憾贪心算法上界
    • ¥15 用visual studi code完成html页面
    • ¥15 聚类分析或者python进行数据分析
    • ¥15 三菱伺服电机按启动按钮有使能但不动作
    • ¥15 js,页面2返回页面1时定位进入的设备
    • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
    • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。