Facebook的整合在一个PHP网站 [英] Facebook Integration in a PHP site

查看:142
本文介绍了Facebook的整合在一个PHP网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的整合现有的PHP的网站与Facebook登录功能。我现在有一个简单的登录注册用户名,密码和电子邮件激活和他们的详细信息存储在MySQL数据库。我要添加其他登录访问Facebook用户。我很担心,我怎么就能够与新的Facebook访问我的当前登录访问集成。在我脑海里主要的是,我所有的页面要求用户先登录,我使用Cookie存储喜欢的绰号会话变量,我使用我的网页上构建内容。

I am trying to integrate my existing php site with Facebook Login functionality. I currently have a simple login registration username, password and email for activation and their details are stored in a MYSQL database. I want to add additional login access for Facebook users. I am worried about how i will be able to integrate my current login access with the new facebook access. Principal on my mind is that all my pages require users to be logged in and i use cookies to store session variables like nickname which i use to build content on my pages.

我感到我已经在网络上研究了许多方法混淆,有些是现在黯然过时,其他人似乎有一半的解决方案。这三个保持再次发生。

I am confused by the many approaches I have researched on the web, some are now sadly obsolete and others seem to be half solutions. These three keep re-occurring.

A)的login.php,Register.php和fbReg.php
在login.php中和register.php出现在使用FB登录时登录/注册创建一个会话,并通过认证的fbReg.php但它只是与页面重定向后黑屏停在那里。

A) Login.php, Register.php and fbReg.php The login.php and register.php appear when logged in using fb login/register to create a session and pass authentication to the fbReg.php but it just stops there with a blank screen after the page is redirected.

(从Register.php片段)

(a snippet from the 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)传统连接验证
http://developers.facebook.com/docs/authentication/connect_auth/
我使用的一些构建测试脚本上面的链接只是为了看看认证会工作。幸运的是在我的网站我有第三方广告或webframes但遵循的步骤,以信建设从我的主登录页面interstitial_page和重定向页面。不过,我不断收到一个空会话。

B) Legacy Connect Auth http://developers.facebook.com/docs/authentication/connect_auth/ Using the above link I constructed some test scripts just to see if the authentication would work. Fortunately on my site I have third party ads or webframes but followed the steps to the letter building a interstitial_page and redirected page from my main login page. However I keep getting a null session.

(从PHP登录页面片段)

(snippet from the php login page)

$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)2.0的OAuth协议
http://developers.facebook.com/docs/authentication/
当我使用这个再次使用下面的脚本:

C) OAuth 2.0 protocol http://developers.facebook.com/docs/authentication/ when I use this again using the following script:

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

我曾尝试和失败,以此来捕获用户信息为我的新的Facebook网站的用户,并将其存储到MySQL数据库则集成了正常web访问用户会话,因为已经assocaiated这是会话变量此认证的Facebook的访问所用的用户体验。

I have tried and failed to use this to capture User info for my new facebook site users and store it to MySQL database then integrate this authenticated facebook access with the 'normal' web access user session since that has assocaiated session variables which are used the the user experience.

任何帮助将是AP preciated,即时通讯挣扎的时刻。

Any help would be appreciated, im struggling at the moment.

感谢迈克尔

我得到了一个空白屏幕上fbReg.php

i get a blank screen on the fbReg.php

推荐答案

我只是做这个上周我的网站非常的事,所以我知道这有多难排序在所有的方法很多,其中有不少已经过时。这是我的方法:

I just did this very thing for my site last week, so I know how hard it is to sort through all of the many methods, many of which are outdated. This was my approach:


  1. 我把我网站上的与Facebook登录链接,重定向到我正常的注册页面。

  1. I placed the "Login in with Facebook" link on my site which redirects to my normal registration page.

<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>


  • 我的注册脚本如下:

  • My registration script looks like this:

    <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 
        ***********************/
    }
    


  • 在我的登录脚本,我设置的用户名,饼干等正常。

  • In my login script, I set the username, etc cookies as normal.

    希望这有助于!

    这篇关于Facebook的整合在一个PHP网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆