Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //NOTE: This code wasn't tested but it should work fine since i almost copied and pasted everything from my previous work i've tested
- //Hope it guides you well enough.
- //
- //Defining some constants i need, replace with yours
- define('HOSTNAME', 'http://localhost/sample');
- define('CURRENT_PAGE', HOSTNAME . '/my_login_page.php');
- //THEN HERE WE GO!
- //
- //
- //Step 1
- //Create a Facebook APP on Facebook developer page
- //Make it public and copy your APP ID and APP SECRET
- //Add your HOSTNAME to the urls allowed to access the app
- //Download facebook sdk and extract to current folder
- //You can browse how to make it public and how to add urls if you don get it at first hand, can't remember the steps right now
- //Step 2
- //Start session, Facebook API needs it and you might need it too.
- //It might be important you understand how PHP session works
- session_start();
- //load Facebook api
- require_once __DIR__ . '/path-to-downloaded-sdk/src/Facebook/autoload.php';
- //Create Facebook object
- $fb = new Facebook\Facebook([
- 'app_id' => 'YOUR_APP_ID',
- 'app_secret' => 'YOUR_APP_SECRET',
- 'default_graph_version' => 'v2.2',
- ]);
- //you'll need to repeat Step 2 on any other you have to use this Facebook API
- //
- //
- //Step 3
- //Assuming this is the same page you want facebook to redirect user after authentication
- //Check if this was a redirect from facebook
- //Am using the 'token' variable to identify a request uniquely, so its what i check for.
- //If the 'token' variable exists in the $_GET then i know its from Facebook and the user has visited this page before
- //I also made a copy of the token in the $_SESSION to match with the one from Facebook to be sure the user is not trying to bypass this step
- if (isset($_GET['token']) and isset($_SESSION['token'])) {
- //This is a redirect from Facebook
- //Compare tokens to avoid unauthorized user access
- if ($_GET['token'] === $_SESSION['token']) {
- $helper = $fb->getRedirectLoginHelper();
- try {
- $accessToken = $helper->getAccessToken();
- if (isset($accessToken)) {
- //User logged in and was sucessfully authenticated!
- //Save accessToken wherever you like, am saving to session
- //This access token is what you use to extract the info you requested for about this user from facebook,
- //as far the session created by facebook api allows you, you can reuse the accessToken anywhere
- $_SESSION['facebook_access_token'] = (string) $accessToken;
- //Extracting info
- // Sets the default fallback access token so we don't have to pass it to each request
- $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
- try {
- //Get fields you need
- $response = $fb->get('/me?fields=id,first_name,last_name,picture,email');
- $userNode = $response->getGraphUser();
- $array = array();
- $array['id'] = $userNode->getId();
- $array['first_name'] = $userNode->getFirstName();
- $array['last_name'] = $userNode->getLastName();
- $pic = $userNode->getPicture();
- if (!empty($pic)) {
- $array['pic_url'] = $pic->getUrl();
- }
- $array['email'] = $userNode->getEmail();
- //Now you have the information you want
- //You might want to save them to database now then redirect the user to his profile or wherever
- //And were done!!!!! phew!!
- } catch (Facebook\Exceptions\FacebookResponseException $e) {
- // When Graph returns an error
- $error = 'Graph returned an error: ' . $e->getMessage();
- } catch (Facebook\Exceptions\FacebookSDKException $e) {
- // When validation fails or other local issues
- $error = 'Facebook SDK returned an error: ' . $e->getMessage();
- }
- }
- } catch (Facebook\Exceptions\FacebookResponseException $e) {
- // When Graph returns an error
- $error = 'Graph returned an error: ' . $e->getMessage();
- } catch (Facebook\Exceptions\FacebookSDKException $e) {
- // When validation fails or other local issues
- $error = 'Facebook SDK returned an error: ' . $e->getMessage();
- }
- } else {
- //User may have tried to bypass authenticaton
- //Do something and show him who's Boss :)
- //Erm.. It's best to just notify him authentication failed
- }
- } else {
- //This is probably User's first visit and not from Facebook
- $helper = $fb->getRedirectLoginHelper();
- $permissions = ['email', 'user_likes']; // optional, check developer page for more permissions you might need
- //Generate token for unique identification of request
- $token = md5(uniqid());
- //token saved to session
- $_SESSION['token'] = $token;
- //Generate login url, this url redirects the user to facebook and does all the authentication
- $loginUrl = $helper->getLoginUrl(CURRENT_PAGE . "?token=$token", $permissions);
- }
- ?>
- <?php
- if (isset($error)) {
- echo $error;
- }
- ?>
- <div style="padding: 20px 20px 20px 20px; text-align: center">
- <a href="<?= $loginUrl ?>" style="padding: 5px 5px 5px 5px; background: blue;">Log in with Facebook</a>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement