How to login using Facebook php sdk
How to login using Facebook php sdk
In this article i’m going to go in a little detail and show you how to create a very simple Facebook login script that you can then use in your own website. Its pretty straight forward really. This can be developed further of course and make more with your users, like signup, conditioning based on gender and many more.
Download Facebook PHP SDK
First thing you need to go to facebook here and download the php Facebook sdk.
https://developers.facebook.com/docs/php/gettingstarted
You should have there a link “Download the PHP SDK”. Click on this and you should be redirected to GIT. Here click to “Download ZIP” file.
You only really need the files from the folder “src“. We will keep that folder for the rest of this article.
Create Facebook APP
Next, you need to create your own app in your Facebook account. On the same page on
https://developers.facebook.com
Go to Apps and click to Create a new App. Follow the steps and you should be alright.
Now that you created the Facebook app, you should have something similar to bellow:
In section App Domains, add your own domain from where you are connection. Add a new platform and choose website, there add your own website url from where you testing / connecting with facebook. I used a subdomain which is pointing to my local PC here.
Login script using Facebook PHP SDK
Once this is done, we only need a little script, I’ve written one below for you guys really fast. You can see more documentation here:
https://developers.facebook.com/docs/php/gettingstarted/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<?php /* * Including the Facebook SDK here */ require_once("src/facebook.php"); // this is the path to your PHP SDK folder $fb_app_id = "464365525701301"; // update with your facebook app ID $fb_app_secret = "eb03205kasjd54asda8901812ada3c3"; // update with your facebook app secret $facebook_config = array( 'appId' => $fb_app_id, 'secret' => $fb_app_secret, ); $facebook = new Facebook($facebook_config); // the function that checks the facebook ID in our users database function check_fb_login($id) { // update with your own mysql details. $mysqli = mysqli_connect($host,$user,$pass,$db) or die("Error " . mysqli_error($link)); if ($stmt = $mysqli->prepare("SELECT fb_id FROM Users WHERE fb_id=?")) { /* bind parameters for markers */ $stmt->bind_param("s", $id); /* execute query */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($facebook_db_id); /* fetch value */ $stmt->fetch(); if($id == $facebook_db_id) { $_SESSION['logged'] = "somevalue"); return 1; } else { return 0; } /* close statement */ $stmt->close(); } $mysqli->close(); } // we check here if we have our user logged into facebook and also has allowed access using our app. $user = $facebook->getUser(); if ($user) { try { // Proceed knowing you have a logged in user who's authenticated. $user_profile = $facebook->api('/me'); // here we check if we have the facebook ID in our users database $check = check_fb_login($user_profile['id']); if ($check) { // if everything is fine, user account exists, redirect your user to your dashboard (this needs changed with your own). header('Location: /?page=dashboard'); } else { // else proceed to registration form, there's no account yet! // again the page needs to be change to your own registration form. header('Location: /?page=register&fb_id='.$user_profile["id"]); } } catch (FacebookApiException $e) { error_log($e); $user = null; } } else { /* * this here is important, make sure you add the array with * the redirect URI so that facebook knows where to return your call back */ $loginUrl = $facebook->getLoginUrl( array('redirect_uri' => 'http://www.findme.yourhowto.net/?page=do-login&type=fb') ); ?> <div class="login-outh"> <a href="<?php echo $login_url; ?>" class="facebook"><i class="fa fa-facebook"></i><span>Signup with Facebook</span></a> </div> <?php } ?> |
In the above code snippet I also commented what you need to know and what you need to change, you can do more than just allowing access to specific section and I did made the example code in such a way.
For example, you have your own Users table and a field called fb_id, each user with their own custom profile pages. What you want is based on their Facebook ID, to match the proper user and show him the correct dashboard page.
For this you need to store your the Facebook ids, probably either have the signup using facebook, or just allow them to login to their account as usual, but have them an option connect to their facebook account and connect their profile to their facebook id, of course, only on the user’s request. Do not force your users!
In the above code snipped I’ve shown you a really quick way of how this is working so that you can use change it based on what you need. I haven’t test it unfortunately, if you find any errors, kindly let me know, I did however checked the code and it should be correct.
What you basically do, is a query and check if the Facebook ID that exists in your database matches the one from your user that logged in, then based on this, allow him access or send him to the registration form.
That’s all for now, sorry again for the tardiness, still working hard on some projects, but will see you again in our next article.