Passed
Branch master (1ffcd5)
by Rakesh
07:09 queued 02:15
created

fb-callback.php (1 issue)

Severity
1
<?php
2
if(!session_id()) {
3
  session_start();
4
}
5
require_once 'lib\Facebook\autoload.php';
6
7
if(isset($_SESSION['fb_access_token'])){
8
  // Get access token from session
9
  $accessToken = $_SESSION['fb_access_token'];
10
  //header('location:member.php');
11
}else{
12
  $appId='362540437809242';
13
  $appSecret='538cd04f971479ff14dc409df2fbcf3b';
14
15
$fb = new Facebook\Facebook([
16
  'app_id' => $appId, // variable with My Facebook App ID
17
  'app_secret' => $appSecret,
18
  'default_graph_version' => 'v3.2',
19
  ]);
20
21
$helper = $fb->getRedirectLoginHelper();
22
23
try {
24
  $accessToken = $helper->getAccessToken();
25
} catch(Facebook\Exceptions\FacebookResponseException $e) {
26
  // When Graph returns an error
27
  echo 'Graph returned an error: ' . $e->getMessage();
28
  exit;
29
} catch(Facebook\Exceptions\FacebookSDKException $e) {
30
  // When validation fails or other local issues
31
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
32
  exit;
33
}
34
35
if (! isset($accessToken)) {
36
  
37
  if ($helper->getError()) {
38
    header('HTTP/1.0 401 Unauthorized');
39
    echo "Error: " . $helper->getError() . "\n";
40
    echo "Error Code: " . $helper->getErrorCode() . "\n";
41
    echo "Error Reason: " . $helper->getErrorReason() . "\n";
42
    echo "Error Description: " . $helper->getErrorDescription() . "\n";
43
  } else {
44
    header('HTTP/1.0 400 Bad Request');
45
    echo 'Bad request';
46
  }
47
  exit;
48
}
49
50
// Logged in
51
//echo '<h3>Access Token</h3>';
52
//($accessToken->getValue());
53
54
// The OAuth 2.0 client handler helps us manage access tokens
55
$oAuth2Client = $fb->getOAuth2Client();
56
57
// Get the access token metadata from /debug_token
58
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
59
echo '<h3>Facebook Photos Challenge</h3>';
60
//var_dump($tokenMetadata);
61
62
// Validation (these will throw FacebookSDKException's when they fail)
63
$tokenMetadata->validateAppId('362540437809242'); // My Facebook App ID
64
// If you know the user ID this access token belongs to, you can validate it here
65
//$tokenMetadata->validateUserId('123');
66
$tokenMetadata->validateExpiration();
67
68
if (! $accessToken->isLongLived()) {
69
  // Exchanges a short-lived access token for a long-lived one
70
  try {
71
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
72
  } catch (Facebook\Exceptions\FacebookSDKException $e) {
73
    echo "<p>Error getting long-lived access token: " . $e->getMessage() . "</p>\n\n";
74
    exit;
75
  }
76
77
  echo '<h3>Long-lived</h3>';
78
  var_dump($accessToken->getValue());
79
}
80
81
$_SESSION['fb_access_token'] = (string) $accessToken;
82
83
try {
84
  // Returns a `Facebook\FacebookResponse` object
85
  $response = $fb->get('/me?fields=id,name', $accessToken);
86
} catch(Facebook\Exceptions\FacebookResponseException $e) {
87
  echo 'Graph returned an error: ' . $e->getMessage();
88
  exit;
89
} catch(Facebook\Exceptions\FacebookSDKException $e) {
90
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
91
  exit;
92
}
93
94
$_SESSION['user'] = $response->getGraphUser();
95
96
if(isset($_SESSION['user']) && isset($_SESSION['fb_access_token'])){
97
  header('Location:member.php');
98
}else{
99
  header('Location:index.php');
100
}
101
}
102
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...