login with facebook php

Solutions on MaxInterview for login with facebook php by the best coders in the world

showing results for - "login with facebook php"
Mads
22 Oct 2020
1/login.php
2  session_start();
3$fb = new Facebook\Facebook([
4  'app_id' => '{app-id}', // Replace {app-id} with your app id
5  'app_secret' => '{app-secret}',
6  'default_graph_version' => 'v3.2',
7  ]);
8
9$helper = $fb->getRedirectLoginHelper();
10
11$permissions = ['email']; // Optional permissions
12$callbackUrl = htmlspecialchars('https://example.com/fb-callback.php');
13$loginUrl = $helper->getLoginUrl($callbackUrl, $permissions);
14
15echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
16
17
18/fb-callback.php
19  session_start();
20$fb = new Facebook\Facebook([
21  'app_id' => '{app-id}', // Replace {app-id} with your app id
22  'app_secret' => '{app-secret}',
23  'default_graph_version' => 'v3.2',
24  ]);
25
26$helper = $fb->getRedirectLoginHelper();
27
28try {
29  $accessToken = $helper->getAccessToken();
30} catch(Facebook\Exceptions\FacebookResponseException $e) {
31  // When Graph returns an error
32  echo 'Graph returned an error: ' . $e->getMessage();
33  exit;
34} catch(Facebook\Exceptions\FacebookSDKException $e) {
35  // When validation fails or other local issues
36  echo 'Facebook SDK returned an error: ' . $e->getMessage();
37  exit;
38}
39
40if (! isset($accessToken)) {
41  if ($helper->getError()) {
42    header('HTTP/1.0 401 Unauthorized');
43    echo "Error: " . $helper->getError() . "\n";
44    echo "Error Code: " . $helper->getErrorCode() . "\n";
45    echo "Error Reason: " . $helper->getErrorReason() . "\n";
46    echo "Error Description: " . $helper->getErrorDescription() . "\n";
47  } else {
48    header('HTTP/1.0 400 Bad Request');
49    echo 'Bad request';
50  }
51  exit;
52}
53
54// Logged in
55echo '<h3>Access Token</h3>';
56var_dump($accessToken->getValue());
57
58// The OAuth 2.0 client handler helps us manage access tokens
59$oAuth2Client = $fb->getOAuth2Client();
60
61// Get the access token metadata from /debug_token
62$tokenMetadata = $oAuth2Client->debugToken($accessToken);
63echo '<h3>Metadata</h3>';
64var_dump($tokenMetadata);
65
66// Validation (these will throw FacebookSDKException's when they fail)
67$tokenMetadata->validateAppId('{app-id}'); // Replace {app-id} with your app id
68// If you know the user ID this access token belongs to, you can validate it here
69//$tokenMetadata->validateUserId('123');
70$tokenMetadata->validateExpiration();
71
72if (! $accessToken->isLongLived()) {
73  // Exchanges a short-lived access token for a long-lived one
74  try {
75    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
76  } catch (Facebook\Exceptions\FacebookSDKException $e) {
77    echo "<p>Error getting long-lived access token: " . $e->getMessage() . "</p>\n\n";
78    exit;
79  }
80
81  echo '<h3>Long-lived</h3>';
82  var_dump($accessToken->getValue());
83}
84
85$_SESSION['fb_access_token'] = (string) $accessToken;
86
87// User is logged in with a long-lived access token.
88// You can redirect them to a members-only page.
89//header('Location: https://example.com/members.php');
queries leading to this page
online php editorhow to create login with facebook in phpexecute php onlinelog in con facebook phpphp online compilersign in with facebook phpphp facebook login classphp editorlogin with facebook using php and mysqlhow to get the data from facebook login in php projectfacebook login php authhow to login with facebook api in phpphp online editorhow to add facebook login in own php projectfacebook login and register in phpphp compiler on 3blinescript login facebook phpfacebook login php apilogin with facebook and google phpgetting the data from facebook login in php projectphp run onlineuse facebook login api with phpsetup project for the facebook login in phpphp code compilerlogin facebook with phplogin with facebook in phphow tologin with facebook on phpfacebook php logindoes php have a compileronline code editor for phpapi login facebook phpfacebook login page php codelogin with facebook phplogin con facebook phpphp login with facebookexecute php code onlineonline php idefacebook login page project phpphp login facebookrun code php onlinerun php onlineweb php editorphp editor onlinecore php with facebook loginrun php file onlinehow to add facebook login to your php projectlog in with facebook in phpphp online iderun php code onlinephp runner onlinephp compilerfacebook login php sdkfacebook login api phpphp run code onlinelogin and signup with facebook using phpphptp editor onlineonline php compilerfacebook login example phpphp facebook loginphp compiler onlinephp code editorfacebook login php login with facebook php