PHP Facebook登陆

PHP Facebook登陆

我们可以使用Facebook登陆来允许用户进入网站。本页面将向您解释如何使用Facebook PHP SDK进行登陆。

使用Facebook登陆

  • 需要访问 https://developers.facebook.com/apps/ 然后点击添加新分组的按钮来创建应用ID。

  • 选择网站

  • 给应用程序命名,并点击创建新的Facebook应用ID

  • 点击创建应用ID

  • 点击跳过快速测试

在最后阶段,会显示如下图像。

PHP Facebook登陆

fbconfig.php文件概述

  • 现在打开fbconfig.php文件,添加你的App ID和App Secret
FacebookSession::setDefaultApplication( 'your app ID','App Secrete ' );
// login helper with redirect_uri
   $helper = new FacebookRedirectLoginHelper('You web address' );

最后的fbconfig.php文件如下所示−

<?php

   session_start();

   // added in v4.0.0
   require_once 'autoload.php';
   use Facebook\FacebookSession;
   use Facebook\FacebookRedirectLoginHelper;
   use Facebook\FacebookRequest;
   use Facebook\FacebookResponse;
   use Facebook\FacebookSDKException;
   use Facebook\FacebookRequestException;
   use Facebook\FacebookAuthorizationException;
   use Facebook\GraphObject;
   use Facebook\Entities\AccessToken;
   use Facebook\HttpClients\FacebookCurlHttpClient;
   use Facebook\HttpClients\FacebookHttpable;

   // init app with app id and secret
   FacebookSession::setDefaultApplication( '496544657159182','e6d239655aeb3e496e52fabeaf1b1f93' );

   // login helper with redirect_uri
   helper = new FacebookRedirectLoginHelper('http://www.tutorialspoint.com/' );

   try {session = helper->getSessionFromRedirect();
   }catch( FacebookRequestExceptionex ) {
      // When Facebook returns an error
   }catch( Exception ex ) {
      // When validation fails or other local issues
   }

   // see if we have a session
   if ( isset(session ) ) {
      // graph api request for user data
      request = new FacebookRequest(session, 'GET', '/me' );
      response =request->execute();

      // get response
      graphObject =response->getGraphObject();
      fbid =graphObject->getProperty('id');           // To Get Facebook ID
      fbfullname =graphObject->getProperty('name');   // To Get Facebook full name
      femail =graphObject->getProperty('email');      // To Get Facebook email ID

      /* ---- Session Variables -----*/
      _SESSION['FBID'] =fbid;
      _SESSION['FULLNAME'] =fbfullname;
      _SESSION['EMAIL'] =femail;

      /* ---- header location after session ----*/
      header("Location: index.php");
   }else {
      loginUrl =helper->getLoginUrl();
      header("Location: ".$loginUrl);
   }
?>

登录页面概述

登录页面用于登录到FB

<?php
   session_start();
   session_unset();

   _SESSION['FBID'] = NULL;_SESSION['FULLNAME'] = NULL;
   $_SESSION['EMAIL'] =  NULL;
   header("Location: index.php");        
?>

Index.php

Index页面如下所示。

<?php
   session_start(); 
?>
<html xmlns:fb = "http://www.facebook.com/2008/fbml">

   <head>
      <title>Login with Facebook</title>
      <link 
         href = "http://www.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" 
         rel = "stylesheet">
   </head>

   <body>
      <?php if (_SESSION['FBID']): ?>      <!--  After user login  -->

         <div class = "container">

            <div class = "hero-unit">
               <h1>Hello <?php echo_SESSION['USERNAME']; ?></h1>
               <p>Welcome to "facebook login" tutorial</p>
            </div>

            <div class = "span4">

               <ul class = "nav nav-list">
                  <li class = "nav-header">Image</li>

                  <li><img src = "https://graph.facebook.com/<?php 
                     echo _SESSION['FBID']; ?>/picture"></li>

                  <li class = "nav-header">Facebook ID</li>
                  <li><?php echo_SESSION['FBID']; ?></li>

                  <li class = "nav-header">Facebook fullname</li>

                  <li><?php echo _SESSION['FULLNAME']; ?></li>

                  <li class = "nav-header">Facebook Email</li>

                  <li><?php echo_SESSION['EMAIL']; ?></li>

                  <div><a href="logout.php">Logout</a></div>

               </ul>

            </div>
         </div>

         <?php else: ?>     <!-- Before login --> 

         <div class = "container">
            <h1>Login with Facebook</h1>
            Not Connected

            <div>
               <a href = "fbconfig.php">Login with Facebook</a>
            </div>

            <div>
               <a href = "http://www.tutorialspoint.com"  
                  title = "Login with facebook">More information about Tutorialspoint</a>
            </div>
         </div>

      <?php endif ?>

   </body>
</html>

这将产生结果。在尝试此示例之前,请在浏览器中注销您的Facebook帐户。

PHP Facebook登陆

退出 Facebook

下面的代码用于退出 Facebook。

<?php 
   session_start();.
   session_unset();

   _SESSION['FBID'] = NULL;_SESSION['FULLNAME'] = NULL;
   $_SESSION['EMAIL'] =  NULL;
   header("Location: index.php");        
?>

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程