authentication - PHP 网站中的 Facebook 集成

标签 authentication oauth facebook legacy

我正在尝试将我现有的 php 网站与 Facebook 登录功能集成。我目前有一个简单的登录注册用户名、密码和用于激活的电子邮件,它们的详细信息存储在 MYSQL 数据库中。我想为 Facebook 用户添加额外的登录权限。我担心如何将当前的登录访问与新的 Facebook 访问集成。我的想法是,我的所有页面都要求用户登录,并且我使用 cookie 来存储 session 变量,例如昵称,我用它来在页面上构建内容。

我对在网上研究过的许多方法感到困惑,有些现在已经过时了,而另一些似乎只是半解决方案。这三个不断重复出现。

A) Login.php、Register.php 和 fbReg.php 使用 fb login/register 登录创建 session 并将身份验证传递给 fbReg.php 时,会出现 login.php 和 register.php,但在页面重定向后,它只是停止在那里并显示空白屏幕。

(来自 Register.php 的片段)

<script type="text/javascript">
FB.init({appId: '<?php echo $fb_app_id; ?>', status: true,
    cookie: true, xfbml: true});

<?php
    if ($errors == 0) {
    ?>
    $('#form_hidden').hide();
<?php } else { ?>
    checkInput('username');
    checkInput('email');
    checkInput('lastname');
    checkInput('firstname');
    checkInput('password');
    checkInput('password2');
<?php } ?>
</script>

B) 旧版 Connect 身份验证 http://developers.facebook.com/docs/authentication/connect_auth/ 使用上面的链接,我构建了一些测试脚本,只是为了看看身份验证是否有效。幸运的是,我的网站上有第三方广告或网络框架,但按照字母的步骤构建了一个 interstitial_page 并从我的主登录页面重定向了页面。但是我不断收到空 session 。

(来自 php 登录页面的片段)

$api_key = "YOUR_API_KEY";
$interstitial_page = "YOUR_SECURE_URL"; //URL with no 3rd party apps

$url='http://www.facebook.com/login.php?api_key=' . $api_key  
. '&session_version=3&next=' . urlencode($interstitial_page)
. '&v=1.0&return_session=1&fbconnect=1'
. '&cancel_url=' . urlencode($interstitial_page);

echo "Welcome to the Old Auth flow";
echo "<p>";

echo("<a href='" . $url . "'>"
. "<img src='http://static.ak.facebook.com/images/"
. "devsite/facebook_login.gif'></a>");

C) OAuth 2.0 协议(protocol) http://developers.facebook.com/docs/authentication/ 当我使用以下脚本再次使用它时:

 https://www.facebook.com/dialog/oauth?
 client_id=YOUR_APP_ID&redirect_uri=YOUR_URL

我尝试使用它来捕获我的新 Facebook 网站用户的用户信息并将其存储到 MySQL 数据库,然后将此经过身份验证的 facebook 访问与“正常”Web 访问用户 session 集成,但失败了,因为它具有关联的 session 变量,这些变量是使用用户体验。

任何帮助将不胜感激,我目前正在努力。

谢谢迈克尔

我的 fbReg.php 出现空白屏幕

最佳答案

我上周刚刚为我的网站做了这件事,所以我知道对所有方法进行排序是多么困难,其中许多方法已经过时了。这是我的方法:

  1. 我在网站上放置了“使用 Facebook 登录”链接,该链接会重定向到我的正常注册页面。

    <a href="http://www.facebook.com/dialog/oauth?client_id=<? echo $FACEBOOK_APP_ID ?>&redirect_uri=http://www.yoursite.com/register.php&scope=email">Log in with Facebook</a>
    
  2. 我的注册脚本如下所示:

    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
    <head></head>
    <body>
    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
    FB.init({appId: '<? echo $FACEBOOK_APP_ID ?>', status: true, cookie: true, xfbml: true});
    FB.Event.subscribe('auth.login', function(response) { window.location.reload(); });
    </script>
    
    function get_facebook_cookie($app_id, $app_secret) 
    {
        $args = array();
        parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
    
        ksort($args);
        $payload = '';
        foreach ($args as $key => $value) {
        if ($key != 'sig') {
        $payload .= $key . '=' . $value;
        }
        }
        if (md5($payload . $app_secret) != $args['sig']) {
        return null;
        }
        return $args;
    }
    
    $cookie = get_facebook_cookie($FACEBOOK_APP_ID, $FACEBOOK_SECRET);
    if($cookie)
    { 
        $access_token = $cookie['access_token'];
    
    }
    $url = 'https://graph.facebook.com/me?access_token='.$access_token;
    $graph = file_get_contents($url);
    if($graph) 
    {
    
        $fbuser = json_decode($graph);
        $userid = $fbuser->id;
        $email = mysql_real_escape_string($fbuser->email);
    
        /********************** 
        I placed code here to check if the  email already exists in my database, which means they are already a member. 
        In that case, I retrieve their username and password from the database and automatically log them 
        in by rerouting them to my login script with their crenditials sent as well.
        ***********************/
    
        /********************** 
        If the email is not already in my database, then I know it's a new user and so I prompt them to make a username here. 
        ***********************/
    }
    //The user either opted to log in using my traditional non-Facebook method, or something went wrong with the FB authentication
    else 
    {
    
        /********************** 
        Show my normal registration form that prompts for username, email, etc 
        ***********************/
    }
    
  3. 在我的登录脚本中,我照常设置用户名等 cookie。

希望这有帮助!

关于authentication - PHP 网站中的 Facebook 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7775079/

相关文章:

javascript - React 应用程序和用户角色

android - TextField controlled controller.addListener(() 在使用 controller.clear 后被多次调用

javascript - Facebook 图表所有共同的 friend

android - Webview 中的 Oauth 请求

android - Instagram API 平台移动 oAuth 错误

java - RestFB在Java中获取事件

android - 当我构建多个 productFlavors 时使用 Facebook SDK INSTALL_FAILED_CONFLICTING_PROVIDER

c# - 验证后读取 WCF 服务中的用户名

java - struts2中使用拦截器时出现NullPointerException

facebook - 部署到 heroku 而无需使用护照回调重定向到本地主机