Nowadays every website has the feature of social logins like Facebook, Twitter, Google Account and more. I got the lots of tutorial request to create a sample code to login with facebook account using the PHP. In this tutorial, I will explain how to log in with the Facebook account using PHP with example and you can also download the complete code. I have added the download link at the end of this tutorial.
Login with Facebook makes easy to connect with users or peoples using Facebook applications for your website. The Facebook SDK for Javascript provides a simple path to integrating users to log in to your websites.
1) Your application uses Facebook Login to authorize users.
2) It’s also provides two login options: Facebook Login and your own website custom login.
Sample users table columns id, email, and username.
1 2 3 4 5 6 | CREATE TABLE users ( id INT PRIMARY KEY AUTO_INCREMENT, email VARCHAR(70), username VARCHAR(100), ); |
Go to the https://developers.facebook.com/apps and create app id and app secret id, just modify in the following code
config.php
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php session_start(); //Facebook App Id and Secret $appID='591405314249348'; $appSecret='9885877b45a30cb235b1228388c17665'; //URL to your website root if($_SERVER['HTTP_HOST']=='localhost'){ $base_url='https://freewebmentor.com/'; }else{ $base_url='https://freewebmentor.com/'; } ?> |
Create a config.php page and add below code after changing database configurations and save.
1 2 3 4 5 6 | <?php mysql_connect("hostname","username","password"); mysql_select_db("db name"); ?> |
If you want to change in your web application index page, just use following code.
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 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Login with Facebook Account</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script> <script type="text/javascript" src="js/getpopup.js"></script> <script type="text/javascript"> $(document).ready(function(){ $('#facebook').click(function(e){ $.oauthpopup({ path: 'login.php', width:600, height:300, callback: function(){ window.location.reload(); } }); e.preventDefault(); }); }); </script> </head> <body> <?php error_reporting(1); session_start(); if(!isset($_SESSION['User']) && empty($_SESSION['User'])) { ?> <img src="images/facebook.png" id="facebook" style="cursor:pointer;float:left;margin-left:550px;" /> <?php } else{ echo '<img src="https://graph.facebook.com/'. $_SESSION['User']['id'] .'/picture" width="30" height="30"/><div>'.$_SESSION['User']['name'].'</div>'; echo '<a href="'.$_SESSION['logout'].'">Logout</a>'; } ?> </body> </html> |
Thanks for giving your valuable time on FreeWebMentor community and follow on Facebook. If you still any question/queries please feel free to comment in below comment section. Do like & share with your friends if you like this post.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: Facebook, facebook login php, facebook php login, https www facebook com login php, login with facebook php, login with facebook php script, PHP, php facebook login, www facebook com php login