// Authorize the user. try { if ( isset( $_SESSION['access_token'] ) ) { // Check if an access token has already been set. $session = new FacebookSession( $_SESSION['access_token'] ); } else { // Get access token from the code parameter in the URL. $session = $helper->getSessionFromRedirect(); } } catch( FacebookRequestException $ex ) { // When Facebook returns an error. print_r( $ex ); } catch( \Exception $ex ) { // When validation fails or other local issues. print_r( $ex ); } if ( isset( $session ) ) { // Retrieve & store the access token in a session. $_SESSION['access_token'] = $session->getToken(); $logoutURL = $helper->getLogoutUrl( $session, 'http://your-app-domain.com/logout' ); // Logged in echo 'Successfully logged in! <a href="' . $logoutURL . '">Logout</a>'; } else { // Generate the login URL for Facebook authentication. $loginUrl = $helper->getLoginUrl(); echo '<a href="' . $loginUrl . '">Login</a>'; }
v.5.0:
try { $accessToken = $helper->getAccessToken(); } catch( Facebook\Exceptions\FacebookSDKException $e ) { // There was an error communicating with Graph echo $e->getMessage(); exit; } if ( isset( $accessToken ) ) { // User authenticated your app! // Save the access token to a session and redirect $_SESSION['facebook_access_token'] = ( string ) $accessToken; // Register or log the user in... exit; } elseif ( $helper->getError() ) { // The user denied the request // You could log this data . . . var_dump( $helper->getError() ); var_dump( $helper->getErrorCode() ); var_dump( $helper->getErrorReason() ); var_dump( $helper->getErrorDescription() ); // You could display a message to the user // being all like, "What? You don't like me?" exit; } // If they've gotten this far, they shouldn't be here http_response_code(400); exit;