0

As you know, steam provides a script to allow people to connect on your site through the steam database. I would like if I refresh the page I don't have to login again. But with steamapi I don't have any idea how to do it.

My Code:

<?php
require 'includes/lightopenid/openid.php';
include_once("db.php");

$_STEAMAPI = "MYAPI";
try {
    $openid = new LightOpenID('http://test/dev1/index.php?id=1');
    if(!$openid->mode) {
        if(isset($_GET['login'])) {
            $openid->identity = 'http://steamcommunity.com/openid/?l=english';
            header('Location: ' . $openid->authUrl());
        } else {

            echo "<form action='?login' method='post'>";
            echo "<input type='image' src='http://cdn.steamcommunity.com/public/images/signinthroughsteam/sits_small.png'>";
            echo "</form>";
        }
    } elseif($openid->mode == 'cancel') {
        echo 'User has canceled authentication!';
    } else {
        if($openid->validate()) {
            $id = $openid->identity;
            $ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
            preg_match($ptn, $id, $matches);

            $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)
            {
                $sql_fetch_id = "SELECT * FROM member WHERE steamid = '$player->steamid'";
                $query_id = mysqli_query($db, $sql_fetch_id);

                if (mysqli_num_rows($query_id) == 0) {
                    $sql_steam = "INSERT INTO member (name, steamid, avatar) VALUES  ('$player->personaname', '$player->steamid', '$player->avatar')";
                    mysqli_query($db, $sql_steam);
                }
                   echo "Welcome back <b>" . $player->personaname . "</br>";
            }
        } else {
            echo "User is not logged in.\n";
        }
    }
} catch(ErrorException $e) {
    echo $e->getMessage();
}
?>
gre_gor
  • 6,669
  • 9
  • 47
  • 52
Kevin1003
  • 21
  • 3

1 Answers1

1

The general approach would be to setup session - in PHP, you can use $_SESSION to store this information. This allows you to store persistent server-side data per user.

Once you retrieve user's SteamID64, save it in the $_SESSION, e.g.:

$_SESSION['steamid'] = $someVal;

You can check if it's set on every subsequent request and act accordingly.

You can see full example here: https://github.com/SmItH197/SteamAuthentication

(Disclaimer: I'm not the author of the lib.)

scholtzm
  • 46
  • 4