【问题标题】:PHP SSO check sessionPHP SSO 检查会话
【发布时间】:2016-03-20 09:11:48
【问题描述】:

我目前正在尝试创建一个单一登录页面,一旦用户使用 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>

【问题讨论】:

    标签: php facebook single-sign-on janrain


    【解决方案1】:

    在上面的示例中,您似乎让客户端 Janrain 社交登录(参与)小部件将身份验证令牌发布到运行 PHP 页面的服务器。在这种情况下,客户端小部件检索到的令牌被提交到您的 PHP 页面,其中 PHP 页面对 Janrain 社交登录“auth_info”API 端点进行服务器端 curl 调用。此调用验证令牌是否有效,并将用户的标准化社交资料数据返回到您的服务器端页面。

    在这种情况下,您的服务器端页面将解析结果,如果结果有效,服务器端页面将设置一个“标志”以指示用户已成功登录。您可以通过多种方式存储认证状态:

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

    归根结底,如何管理已验证状态是由您决定的实现细节。 Janrain Social Login (Engage) 小部件简化并规范了社交登录过程,并允许您作为开发人员不必为多个社交登录提供程序实现所有不同的 API。 Janrain 社交登录小部件不维护身份验证状态。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      相关资源
      最近更新 更多