Advertisement
Guest User

Index Fb

a guest
Sep 24th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. <?php
  2. // Include FB config file && User class
  3. require_once 'fbConfig.php';
  4. include_once 'User.php';
  5.  
  6. if(isset($accessToken)){
  7. if(isset($_SESSION['facebook_access_token'])){
  8. $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
  9. }else{
  10. // Put short-lived access token in session
  11. $_SESSION['facebook_access_token'] = (string) $accessToken;
  12.  
  13. // OAuth 2.0 client handler helps to manage access tokens
  14. $oAuth2Client = $fb->getOAuth2Client();
  15.  
  16. // Exchanges a short-lived access token for a long-lived one
  17. $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
  18. $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
  19.  
  20. // Set default access token to be used in script
  21. $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
  22. }
  23.  
  24. // Redirect the user back to the same page if url has "code" parameter in query string
  25. if(isset($_GET['code'])){
  26. header('Location: ./');
  27. }
  28.  
  29. // Getting user facebook profile info
  30. try {
  31. $profileRequest = $fb->get('/me?fields=name,first_name,last_name,email,link,gender,locale,picture');
  32. $fbUserProfile = $profileRequest->getGraphNode()->asArray();
  33. } catch(FacebookResponseException $e) {
  34. echo 'Graph returned an error: ' . $e->getMessage();
  35. session_destroy();
  36. // Redirect user back to app login page
  37. header("Location: ./");
  38. exit;
  39. } catch(FacebookSDKException $e) {
  40. echo 'Facebook SDK returned an error: ' . $e->getMessage();
  41. exit;
  42. }
  43.  
  44. // Initialize User class
  45. $user = new User();
  46.  
  47. // Insert or update user data to the database
  48. $fbUserData = array(
  49. 'oauth_provider'=> 'facebook',
  50. 'oauth_uid' => $fbUserProfile['id'],
  51. 'first_name' => $fbUserProfile['first_name'],
  52. 'last_name' => $fbUserProfile['last_name'],
  53. 'email' => $fbUserProfile['email'],
  54. 'gender' => $fbUserProfile['gender'],
  55. 'locale' => $fbUserProfile['locale'],
  56. 'picture' => $fbUserProfile['picture']['url'],
  57. 'link' => $fbUserProfile['link']
  58. );
  59. $userData = $user->checkUser($fbUserData);
  60.  
  61. // Put user data into session
  62. $_SESSION['userData'] = $userData;
  63.  
  64. // Get logout url
  65. $logoutURL = $helper->getLogoutUrl($accessToken, $redirectURL.'logout.php');
  66.  
  67. // Render facebook profile data
  68. if(!empty($userData)){
  69. $output = '<h1>Facebook Profile Details </h1>';
  70. $output .= '<img src="'.$userData['picture'].'">';
  71. $output .= '<br/>Facebook ID : ' . $userData['oauth_uid'];
  72. $output .= '<br/>Name : ' . $userData['first_name'].' '.$userData['last_name'];
  73. $output .= '<br/>Email : ' . $userData['email'];
  74. $output .= '<br/>Gender : ' . $userData['gender'];
  75. $output .= '<br/>Locale : ' . $userData['locale'];
  76. $output .= '<br/>Logged in with : Facebook';
  77. $output .= '<br/><a href="'.$userData['link'].'" target="_blank">Click to Visit Facebook Page</a>';
  78. $output .= '<br/>Logout from <a href="'.$logoutURL.'">Facebook</a>';
  79. }else{
  80. $output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';
  81. }
  82.  
  83. }else{
  84. // Get login url
  85. $loginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);
  86.  
  87. // Render facebook login button
  88. $output = '<a href="'.htmlspecialchars($loginURL).'"><img src="images/fblogin-btn.png"></a>';
  89. }
  90. ?>
  91. <html>
  92. <head>
  93. <title>Login with Facebook using PHP by CodexWorld</title>
  94. <style type="text/css">
  95. h1{font-family:Arial, Helvetica, sans-serif;color:#999999;}
  96. </style>
  97. </head>
  98. <body>
  99. <!-- Display login button / Facebook profile information -->
  100. <div><?php echo $output; ?></div>
  101. </body>
  102. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement