PHP SSO检查会话

时间:2016-03-20 09:11:48

标签: php facebook single-sign-on janrain

我目前正在尝试创建单点登录页面,一旦用户使用Facebook或Google+登录,用户将被重定向到提供商页面,然后一旦登录成功,它将重定向回index.php现场。现在问题是我想知道有没有办法检查用户是否已登录。用户登录登录按钮后的示例应更改为“注销”。我能知道怎么做吗?提前谢谢。

<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 个答案:

答案 0 :(得分:1)

在上面的示例中,您似乎正在使用客户端Janrain社交登录(Engage)小组件将身份验证令牌发布到运行PHP页面的服务器。在这种情况下,客户端小部件检索到的令牌将提交到您的PHP页面,其中PHP页面向Janrain社交登录&#34; auth_info&#34;进行服务器端卷曲调用。 API终点。此调用验证令牌是否有效,并将用户的规范化社交配置文件数据返回到服务器端页面。

在这种情况下,您的服务器端页面将解析结果,如果它是有效的,服务器端页面将设置一个&#34;标记&#34;表示用户已成功登录。有多种方法可以存储身份验证状态:

  • 最安全的是某种形式的服务器端会话变量 您的服务器端代码始终验证。
  • 将一些代码写回作为设置cookie或结果的结果的一部分 localStorage值,表示用户已通过身份验证。

最终,您如何管理经过身份验证的状态是一个由您自己决定的实现细节。 Janrain社交登录(Engage)小部件简化了社交登录过程并使其规范化,并允许您作为开发人员不必为多个社交登录提供者实现所有不同的API。 Janrain Social Login小部件不保持身份验证状态。

具体来说,回答有关登录/注销按钮的问题 - 您将拥有一个客户端Javascript,它检测到cookie的设置并切换按钮上的text / css,或者您可以让服务器端页面注入页面上必需的客户端Javascript代码。更强大的选项可能会使用AJAX类型调用来发布令牌并接收结果,然后更新按钮状态。