2016-03-20 48 views
0

我目前正在嘗試創建一個單一登錄頁面,一旦用戶使用Facebook或Google+登錄,用戶將被重定向到提供者頁面,然後一旦登錄成功,它將重定向回index.php現場。現在的問題是我想知道是否有任何方法來檢查用戶是否已登錄。用戶登錄登錄按鈕後的示例應該更改爲註銷。我可以知道如何做到這一點。提前致謝。PHP SSO檢查會話

<nav id="menu"> <!-- Navigation --> 
    <ul id="tabs"> <!-- unordered list --> 
     <li><a href="index.html">Home</a></li> 
     <li><a href="about.html">About</a></li> 
     <li><a href="contact.html">Contact Us</a></li> 
     <li><a href="login.php">Login</a></li> 
     <ul class="nav navbar-nav navbar-right" > 
      <li style="float:right;list-style-type:none;"> 
       <a class="janrainEngage" href="#"> 
        <img src="<?php echo $_GET['photo']; ?> " height="30" width="30"/> 
        <?php echo $_GET['name'];?> 
       </a> 
      </li> 
     </ul> 
    </ul> 
</nav> 

這是我的login.php代碼:

<?php 
// Below is a very simple and verbose PHP script that implements the Engage 
// token URL processing and some popular Pro/Enterprise examples. The code below 
// assumes you have the CURL HTTP fetching library with SSL. 
require('helpers.php'); 

ob_start(); 

// PATH_TO_API_KEY_FILE should contain a path to a plain text file containing 
// only your API key. This file should exist in a path that can be read by your 
// web server, but not publicly accessible to the Internet. 
$janrain_api_key = trim(file_get_contents('apiKey.txt')); 

// Set this to true if your application is Pro or Enterprise. 
$social_login_pro = false; 

// Step 1: Extract token POST parameter 
$token = $_POST['token']; 

if ($token) { 
    // Step 2: Use the token to make the auth_info API call. 
    $post_data = array(
     'token' => $token, 
     'apiKey' => $janrain_api_key, 
     'format' => 'json' 
    ); 

    if ($social_login_pro) { 
     $post_data['extended'] = 'true'; 
    } 

    $curl = curl_init(); 
    $url = 'https://rpxnow.com/api/v2/auth_info'; 
    $result = curl_helper_post($curl, $url, $post_data); 
    if ($result == false) { 
     curl_helper_error($curl, $url, $post_data); 
     die(); 
    } 
    curl_close($curl); 

    // Step 3: Parse the JSON auth_info response 
    $auth_info = json_decode($result, true); 

    if ($auth_info['stat'] == 'ok') { 
     echo "\n auth_info:"; 
     echo "\n"; var_dump($auth_info); 

     // Pro and Enterprise API examples 
     if ($social_login_pro) { 
      include('social_login_pro_examples.php'); 
     } 

     // Step 4: Your code goes here! Use the identifier in 
     // $auth_info['profile']['identifier'] as the unique key to sign the 
     // user into your system. 
     //echo '<pre>'.print_r($auth_info).'</pre>'; 
     $name = $auth_info['profile']['displayName']; 
     $address = $auth_info['profile']['address']['formatted']; 
     $photo = $auth_info['profile']['photo']; 
     $redirect = "http://localhost:8012/cm0655-assignment/index.php?photo=".$photo; 
     header('Location: '.$redirect); 
    } else { 
     // Handle the auth_info error. 
     output('An error occurred', $auth_info); 
     output('result', $result); 
    } 
} else { 
    echo 'No authentication token.'; 
} 
$debug_out = ob_get_contents(); 
ob_end_clean(); 
?> 
<html> 
    <head> 
     <title>Janrain Token URL Example</title> 
    </head> 
    <body> 
     <pre><?php echo $debug_out; ?></pre> 
    </body> 
</html> 

回答

1

在你上面的例子看來你有客戶端的Janrain社會登錄(搞)的Widget發佈身份驗證令牌到你的服務器運行PHP頁面。在這種情況下,客戶端小部件檢索到的令牌被提交給PHP頁面,PHP頁面將服務器端curl調用到Janrain Social Login「auth_info」API端點。此調用驗證令牌是否有效,並將用戶的規範化社交配置文件數據返回到服務器端頁面。

在這種情況下,您的服務器端頁面將解析結果,如果它是有效的服務器端頁面將設置一個「標誌」,以指示用戶已成功登錄。有很多方法可以存儲認證狀態:

  • 最安全的是某種形式的服務器端會話變量 您的服務器端代碼驗證總是對的。
  • 寫回一些代碼作爲結果的一部分,該結果設置了cookie或 localStorage值以指示用戶已通過身份驗證。

最終,您如何管理已驗證的狀態是一個由您決定的實現細節。 Janrain社交登錄(Engage)小部件簡化了社交登錄過程並使其標準化,並允許您作爲開發人員不必爲多個社交登錄提供程序實現所有不同的API。 Janrain Social Login小部件不保持身份驗證狀態。

具體來說,回答您有關登錄/註銷按鈕的問題 - 您將有一個客戶端Javascript檢測到cookie的設置並切換按鈕上的文本/ css,或者您可以使用服務器端頁面將必要的客戶端Javascript代碼注入頁面。更健壯的選項可能會使用AJAX類型的調用來發布令牌並接收結果並隨後更新按鈕狀態。