简体   繁体   English

使用PHP SDK登录Facebook

[英]Facebook login using PHP SDK

I am trying to login with FB using codeigniter & the FB PHP SDK. 我正在尝试使用codeigniter和FB PHP SDK登录FB。

but it keeps giving me the same login with facebook link, it's not catching if the user is logged in, Am I missing anything like sessions or something? 但它始终为我提供与Facebook链接相同的登录名,如果用户已登录,则无法捕获,我是否缺少会话或其他内容?

And since we are at it how can I get the extended session & what data should I save to publish to pages while user is offline? 既然如此,我如何获得扩展的会话以及在用户离线时应保存哪些数据以发布到页面上?

here is my controller: 这是我的控制器:

function __construct () {
    parent::__construct();      
    $this->load->helper('url_helper');
    $this->load->add_package_path(APPPATH.'third_party/facebook/');
    $this->config->load('facebook', TRUE);
    $this->load->library('facebook', array( 'appId'  => $this->config->item('appId', 'facebook'), 'secret' => $this->config->item('secret', 'facebook') ) );
}

public function index() {
    $data['LoginURL']   = $this->facebook->getLoginUrl( array('scope' => 'manage_pages') );
    $data['user']       = $this->facebook->getUser();
    if ($data['user']) {
      try {
        $data['user_profile']   = $this->facebook->api('/me');
        $data['AccessToken']    = $this->facebook->getAccessToken();
      } catch (FacebookApiException $e) {
        // The access token we have is not valid
        $data['user'] = null;
      }
    }
    $this->load->view('fb_welcome', $data);
}

And here is the view: 这是视图:

<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<?php if ( $user ): ?>      
    User <em><?php echo $user ?></em> is Logged in, AccessToken is <?php echo $AccessToken; ?>
    <?php print_r($user_profile); ?>
<?php else: ?>
    <?php echo anchor($LoginURL, "Login with facebook"); ?>
<?php endif; ?>

</body>
</html>

I dont know code igniter, but hopefully this should prove a good starting point, just link to your fb sdk in the include, replace the vars with your own and it should give the user a popup when they are not logged in: 我不知道代码点火器,但是希望这可以证明是一个很好的起点,只需在include中链接到您的fb sdk,用您自己的var替换var,当用户未登录时,它应该会给用户一个弹出窗口:

// include the FB SDK
require_once(APPLICATION_PATH . '/../library/facebook/facebook.php');

// create the application instance
$facebook = new Facebook(array(
    'appId'  => $this->facebookConfig->appid,
    'secret' => $this->facebookConfig->appsecret
));

// get the user id
$user = $facebook->getUser();
if(!empty($user)) {
    // We have a user ID, so probably a logged in user.
    // If not, we'll get an exception, which we handle below.
    try {

        // do something

    } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $params = array(
            'scope'         => $this->permissions,
            'redirect_uri'  => $this->facebookConfig->applink
        );
        $loginUrl = $facebook->getLoginUrl($params);

        // log errors
        error_log($e->getType());
        error_log($e->getMessage());

        // redirect if not logged in or not enough permissions
        //echo "<script>top.location=\"".$loginUrl."\";</script>";die;
    }

    // Give the user a logout link 
    //echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
} else {

    $params = array(
            'scope'         => $this->permissions,
            'redirect_uri'  => $this->facebookConfig->applink
        );
    $loginUrl = $facebook->getLoginUrl($params);

    // log errors
    error_log($e->getType());
    error_log($e->getMessage());

    // redirect if not logged in or not enough permissions
    echo "<script>top.location=\"".$loginUrl."\";</script>";die;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM