2011-03-26 47 views
0

1)用户点击这是做FB连接的正确方法?

2) '用Facebook登录' 将用户重定向到http://www.facebook.com/dialog/oauth?client_id=xx&redirect_url=xxx&etcetc

3)用户点击允许,返回到我们的网站。

4)我们能够获取用户的姓名,电子邮件和我们有许可其他设置,使用我们能够在我们网站上创建一个帐户,他随机密码。

5)下一次用户点击这个按钮,如果我们已经有他的账户上我们的网站(使用电子邮件地址进行比较),我们简单地自动登录他在第4步

+0

为什么不使用Facebook PHP SDK? https://github.com/facebook/php-sdk/ – 2011-03-26 02:05:43

+0

@Russel我使用笨,不知道这将是多么容易整合。通过这种方法使用SDK有什么特别的优势吗? – 2011-03-26 02:07:29

+0

不知道CI,但是,在Kohana中,您可以通过将其放置在供应商文件夹中来轻松添加*供应商*代码。也许CI有类似的东西。至于优点,这是一个开源,经过尝试和测试的系统,被许多人所使用。因此它将比定制的实现更强大和更安全。 – 2011-03-26 04:16:34

回答

1

绝对使用SDK。它的好处是它是一个在野外测试和使用的库。永远不会重建轮时,你不必(你会发现,你完成更多工作;))。

我最终什么事在CI做的是Facebook的PHP SDK添加到我的图书馆目录并更改Facebook__construct功能是:

public function __construct() 
{ 
    $ci =& get_instance(); 

    $this->setAppId($ci->config->item('fb_appId')); 
    $this->setApiSecret($ci->config->item('fb_secret')); 
    $this->setCookieSupport($ci->config->item('fb_cookie')); 
    $this->setBaseDomain($ci->config->item('fb_domain')); 
    $this->setFileUploadSupport($ci->config->item('fb_upload')); 
} 

然后,

  • 我将以上所有键/值对添加到config/config.php
  • 将facebook库添加到自动载入列表

这项工作完成,我可以从任何地方通过$this->facebook访问FB API在我的应用程序。

说了这么多,这是所有预先2.0,所以我不能完全肯定,如果需要(我使用Yii现在,这主要是为什么我不知道,如果变化是变化会是什么需要:))。

希望有所帮助。

编辑:

按照要求,我最终加入UserModel类(扩展Model)。我支持多个登录提供者,所以我不会发布整个事情。但是,这是它的要点:

class UserModel extends Model 
{ 
     private $m_user; 

     public function UserModel() 
     { 
       parent::Model(); 

       $this->m_user = null; 

       $session = $this->facebook->getSession(); 
       if($session) 
       { 
         if($this->facebook->api('/me') != null) 
         { 
           $this->m_user = $this->facebook->api('/me'); 
         } 
       } 
     } 

     public function getUser() 
     { 
       return $this->m_user; 
     } 

     public function isLoggedIn() 
     { 
       return $this->getUser() != null; 
     } 

     // returns either the login or logout url for the given provider, relative to the 
     // state that the current user object is in 
     public function getActionUrl() 
     { 
       if($this->isLoggedIn()) 
       { 
         return $this->facebook->getLogouturl(); 
       } 
       else 
       { 
         return $this->facebook->getLoginUrl(array('next'=>currentUrl(), 'cancel'=>currentUrl(), 'req_perms'=>null, 'display'=>'popup')); 
       } 
     } 
} 

然后,我添加了一个登录窗口小部件,它只是包含此:

<div id="header-login"> 
     <!-- todo: this won't work with konqueror atm (fb script bugs) - check 'em out later --> 
     <?php if(!$user->isLoggedIn()): ?> 
     <fb:login-button perms="email"><?php echo lang('fb_login'); ?></fb:login-button> 
     <?php else: ?> 
     <a onclick="FB.logout();" class="fb_button fb_button_medium"><span class="fb_button_text">Logout</span></a> 
     <?php endif; ?> 
</div> 

第二个编辑:

对不起,它是一个而自从我写这篇文章以来,所以我不得不回头弄清楚它是如何实现的:P经过快速的grep,我发现我实际上并没有在任何地方使用getActionUrl。我添加了一些客户端脚本来收听FB登录/注销事件:

google.setOnLoadCallback(on_load); 
google.load("jquery", "1.4.4"); 

window.fbAsyncInit = function() { 
     FB.init({appId: '[id]', status: true, cookie: true, xfbml: true}); 
     FB.Event.subscribe('auth.login', on_fb_login); 
     FB.Event.subscribe('auth.logout', on_fb_logout); 
}; 

function on_load() 
{ 
     // force all anchors with the rel tag "ext" to open in an external window 
     // (replaces target= functionality) 
     $('a[rel="ext"]').click(function(){ 
       window.open(this.href); 
       return false; 
     }); 
} 

function on_fb_login() 
{ 
     location.reload(); 
} 

function on_fb_logout() 
{ 
     location.reload(); 
} 
+0

你可以分享你将如何实现登录/ Facebook连接使用该方法? – 2011-03-26 02:35:41

相关问题