2017-01-30 157 views
0

如您所知,蒸汽提供腳本以允許人們通過蒸汽數據庫連接到您的站點。 我想如果我刷新頁面,我不必再次登錄。但與steamapi我不知道如何去做。Steam驗證會話

我的代碼:

<?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(); 
} 
?> 

回答

1

一般的做法是建立session - 在PHP中,你可以使用$_SESSION來存儲這些信息。這允許您爲每個用戶存儲持久的服務器端數據。

當你獲得用戶的SteamID64,它保存在$_SESSION,例如:

$_SESSION['steamid'] = $someVal;

,如果它在每一個後續請求設置您可以檢查並採取相應的行動。

你可以看到完整的例子在這裏:https://github.com/SmItH197/SteamAuthentication

(免責聲明:我不是LIB的作者)