PHP 网站中的 Facebook 集成

发布于 2024-12-09 20:00:24 字数 2285 浏览 0 评论 0原文

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

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

A) Login.php、Register.php 和 fbReg.php 使用 fb login/register 登录创建会话并将身份验证传递给 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 并从我的主登录页面重定向了页面。但是我不断收到空会话。

(来自 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 协议 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 访问用户会话集成,但失败了它具有关联的会话变量,用于用户体验。

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

谢谢迈克尔,

我在 fbReg.php 上看到一个空白屏幕

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

(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) 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.

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

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.

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

Thanks Michael

i get a blank screen on the fbReg.php

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ま昔日黯然 2024-12-16 20:00:24

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

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

    使用 Facebook 登录;
    
  2. 我的注册脚本如下所示:

    
    <头>
    <正文>
    
    <脚本 src="http://connect.facebook.net/en_US/all.js"> <脚本> FB.init({appId: '', status: true, cookie: true, xfbml: true}); FB.Event.subscribe('auth.login', function(response) { window.location.reload(); }); 函数 get_facebook_cookie($app_id, $app_secret) { $args = 数组(); parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args); ksort($args); $有效负载=''; foreach ($args as $key => $value) { if ($key != 'sig') { $payload .= $key 。 '=' 。 $值; } } if (md5($payload . $app_secret) != $args['sig']) { 返回空值; } 返回$args; } $cookie = get_facebook_cookie($FACEBOOK_APP_ID, $FACEBOOK_SECRET); 如果($cookie) { $access_token = $cookie['access_token']; } $url = 'https://graph.facebook.com/me?access_token='.$access_token; $graph = file_get_contents($url); 如果($图) { $fbuser = json_decode($graph); $userid = $fbuser->id; $email = mysql_real_escape_string($fbuser->email); /************************ 我在这里放置了代码来检查电子邮件是否已存在于我的数据库中,这意味着他们已经是会员。 在这种情况下,我从数据库中检索他们的用户名和密码并自动记录它们 通过将他们重新路由到我的登录脚本并发送他们的凭据。 **********************/ /************************ 如果该电子邮件尚未存在于我的数据库中,那么我就知道这是一个新用户,因此我提示他们在此处创建一个用户名。 **********************/ } //用户要么选择使用我传统的非Facebook方法登录,要么FB身份验证出现问题 别的 { /************************ 显示我的正常注册表单,提示输入用户名、电子邮件等 **********************/ }
  3. 在我的登录脚本中,我照常设置用户名等 cookie。

希望这有帮助!

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. 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>
    
  2. 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 
        ***********************/
    }
    
  3. In my login script, I set the username, etc cookies as normal.

Hope this helps!

拥醉 2024-12-16 20:00:24

我不知道您是否在谈论社交插件,但如果您是在谈论这些 Facebook 插件-ins 在页面中内置了生成器,它将生成您需要的代码。

如果您不是在寻找插件,而是在寻找用户信息,我仍然建议您运行生成器来获取代码,并查看 Facebook 如何获取用户信息。

遵循简单脚本的路径应该可以让您到达您需要的地方。

I can't tell if you are talking about the social plug-ins, but if you are, these Facebook Plug-ins have generators built into the pages which will generate the code you need.

If you are not looking for plugins but instead the information for the users, I still recommend that you run the generators to get the code and see how Facebook gets the information for the users.

Following the path of the simple scripts should get you to where you need to be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文