2015-10-06 76 views
0

所以我也想爲我的網站做一個蒸汽登錄按鈕,但它不會重定向到我想要的。相反,它只是將用戶導向一個帶有用戶名和註銷按鈕的普通站點。我沒有編寫我剛剛拷貝的其他代碼,只是爲了試用它。我試圖將其引導回index.php,但它並不需要。 下面是代碼:Steam OpenID PHP和HTML

<?php 
         error_reporting(E_ERROR | E_PARSE | E_WARNING); 

$user = new user; 
$user->apikey = ""; // put your API key here 
$user->domain = "localhost/index.php"; // put your domain 


class user 
{ 
    public static $apikey; 
    public static $domain; 

    public function GetPlayerSummaries ($steamid) 
    { 
     $response = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $this->apikey . '&steamids=' . $steamid); 
     $json = json_decode($response); 
     return $json->response->players[0]; 
    } 

    public function signIn() 
    { 
     require_once 'openid.php'; 
     $openid = new LightOpenID($this->domain);// put your domain 
     if(!$openid->mode) 
     { 
      $openid->identity = 'http://steamcommunity.com/openid'; 
      header('Location: ' . $openid->authUrl()); 
     } 
     elseif($openid->mode == 'cancel') 
     { 
      print ('User has canceled authentication!'); 
     } 
     else 
     { 
      if($openid->validate()) 
      { 
       preg_match("/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/", $openid->identity, $matches); // steamID: $matches[1] 
       setcookie('steamID', $matches[1], time()+(60*60*24*7), '/'); // 1 week 
       header('Location: /'); 
       exit; 
      } 
      else 
      { 
       print ('fail'); 
      } 
     } 
    } 
} 

if(isset($_GET['login'])) 
{ 
    $user->signIn(); 
} 
if (array_key_exists('logout', $_POST)) 
{ 
    setcookie('steamID', '', -1, '/'); 
    header('Location: /'); 
} 


if(!$_COOKIE['steamID']) 
{ 
    print ('<form action="?login" method="post"> 
     <input type="image" src="http://cdn.steamcommunity.com/public/images/signinthroughsteam/sits_large_border.png"/> 
     </form>'); 
} 
else 
{ 
    print('<form method="post"><button title="Logout" name="logout">Logout</button></form>'); 
    echo $user->GetPlayerSummaries($_COOKIE['steamID'])->personaname; 
} 




?> 

我刪除了Apikey,因爲我不知道它保存只是把它放到網上給大家看這個樣子。 謝謝大家:)有一個美好的一天。對不起,我的語法不是很好。再次感謝:d

回答

0

我使用相同的代碼爲我的蒸汽網站 See here.

  preg_match("/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/", $openid->identity, $matches); // steamID: $matches[1] 
      setcookie('steamID', $matches[1], time()+(60*60*24*7), '/'); // 1 week 
      header('Location: /'); 
      exit; 

在代碼方面,應該有一個修復。 我似乎無法找到我的PHP,因爲我使我的2個PHP鏈接非常複雜,並使用框架將它們與其中一個連接起來。

正如你所說

{ 
print('<form method="post"><button title="Logout" name="logout">Logout</button></form>'); 
echo $user->GetPlayerSummaries($_COOKIE['steamID'])->personaname; 
} 

礦是由這樣的,與此PHP代碼加載在登錄/註銷按鈕。不確定這是否會幫助你! 如果您使用此代碼,請確保您已爲登錄網站啓用allow_url_fopen

$ sign_inout = file_get_contents('http://kuramacraft.net/steam/signinout.php');

echo $ sign_inout;

感謝您花時間閱讀本文!