Login with Facebook using PHP SDK with mysql download
In this Post We Will Explain About is Login with Facebook using PHP SDK with mysql download With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Login With Facebook using PHP and MySQLExample
In this post we will show you Best way to implement Login System with Facebook with PHP and MySQL step by step, hear for PHP Script – Login with Facebook using facebook-php-sdkwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.
Login with Facebook is a simple and powerful way to integrate step by step registration and login system on the web application or website. Facebook is a very most popular social network as well as most of the persons generally have a Facebook account.simple step by step Facebook Login allow all the persons to sign into your website and get some public information using their Facebook account credentials like as a fbid without sign up on your app’s website.
Step 1 : File and Folder Structures
facebook-php-sdk/ Facebook SDK v5 for PHP Person.php fbConfig.php index.php form_logout.php images/ fblogin-btn.png
Step 2 : Database Table Creation
CREATE TABLE `persons` ( `id` int(11) NOT NULL AUTO_INCREMENT, `oauth_provider` enum('','facebook','google','twitter') COLLATE utf8_unicode_ci NOT NULL, `oauth_uid` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `per_fname` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `per_lname` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `gender` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `locale` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `picture` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `link` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `created` datetime NOT NULL, `modified` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Step 3 : Person Class (Person.php)
<?php class Person { private $dbHost = "localhost"; private $dbPersonname = "root"; private $dbPassword = "live****************"; private $dbName = "live24u"; private $personTbl = 'persons'; function __construct(){ if(!isset($this->db)){ $conn = new mysqli($this->dbHost, $this->dbPersonname, $this->dbPassword, $this->dbName); if($conn->connect_error){ die("Failed to connect with MySQL: " . $conn->connect_error); }else{ $this->db = $conn; } } } function checkPerson($personData = array()){ if(!empty($personData)){ $prevQuery = "SELECT * FROM ".$this->personTbl." WHERE oauth_provider = '".$personData['oauth_provider']."' AND oauth_uid = '".$personData['oauth_uid']."'"; $prevResult = $this->db->query($prevQuery); if($prevResult->num_rows > 0){ $query = "UPDATE ".$this->personTbl." SET per_fname = '".$personData['per_fname']."', per_lname = '".$personData['per_lname']."', email = '".$personData['email']."', gender = '".$personData['gender']."', locale = '".$personData['locale']."', picture = '".$personData['picture']."', link = '".$personData['link']."', modified = '".date("Y-m-d H:i:s")."' WHERE oauth_provider = '".$personData['oauth_provider']."' AND oauth_uid = '".$personData['oauth_uid']."'"; $update = $this->db->query($query); }else{ $query = "INSERT INTO ".$this->personTbl." SET oauth_provider = '".$personData['oauth_provider']."', oauth_uid = '".$personData['oauth_uid']."', per_fname = '".$personData['per_fname']."', per_lname = '".$personData['per_lname']."', email = '".$personData['email']."', gender = '".$personData['gender']."', locale = '".$personData['locale']."', picture = '".$personData['picture']."', link = '".$personData['link']."', created = '".date("Y-m-d H:i:s")."', modified = '".date("Y-m-d H:i:s")."'"; $insert = $this->db->query($query); } $result = $this->db->query($prevQuery); $personData = $result->fetch_assoc(); } return $personData; } } ?>
Step 4 : Facebook API Configuration (fbConfig.php)
<?php if(!session_id()){ session_start(); } require_once __DIR__ . '/facebook-php-sdk/autoload.php'; use Facebook\Facebook; use Facebook\Exceptions\FacebookResponseException; use Facebook\Exceptions\FacebookSDKException; $appId = '1299799058656898536568'; $appSecret = 'ALive24udotcome4587SSSSJJT'; $redirectURL = 'http://localhost/facebook_login_with_php/'; $fbPermissions = array('email'); $fb = new Facebook(array( 'app_id' => $appId, 'app_secret' => $appSecret, 'default_graph_version' => 'v2.2', )); $helper = $fb->getRedirectLoginHelper(); try { if(isset($_SESSION['get_fb_access_token'])){ $getaccesstoken = $_SESSION['get_fb_access_token']; }else{ $getaccesstoken = $helper->getAccessToken(); } } catch(FacebookResponseException $e) { echo 'simple Graph data returned an error: ' . $e->getMessage(); exit; } catch(FacebookSDKException $e) { echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } ?>
Step 5 : Login and Profile Information (index.php)
<?php require_once 'fbConfig.php'; require_once 'Person.php'; if(isset($getaccesstoken)){ if(isset($_SESSION['get_fb_access_token'])){ $fb->setDefaultAccessToken($_SESSION['get_fb_access_token']); }else{ $_SESSION['get_fb_access_token'] = (string) $getaccesstoken; $oAuth2Client = $fb->getOAuth2Client(); $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['get_fb_access_token']); $_SESSION['get_fb_access_token'] = (string) $longLivedAccessToken; $fb->setDefaultAccessToken($_SESSION['get_fb_access_token']); } if(isset($_GET['code'])){ header('Location: ./'); } try { $prequest = $fb->get('/me?fields=name,per_fname,per_lname,email,link,gender,locale,picture'); $fbPersonProfile = $prequest->getGraphNode()->asArray(); } catch(FacebookResponseException $e) { echo 'Graph returned an error: ' . $e->getMessage(); session_destroy(); header("Location: ./"); exit; } catch(FacebookSDKException $e) { echo 'simple Facebook SDK Pakainfo.com returned an error: ' . $e->getMessage(); exit; } $user = new Person(); $fbPersonData = array( 'oauth_provider'=> 'facebook', 'oauth_uid' => $fbPersonProfile['id'], 'per_fname' => $fbPersonProfile['per_fname'], 'per_lname' => $fbPersonProfile['per_lname'], 'email' => $fbPersonProfile['email'], 'gender' => $fbPersonProfile['gender'], 'locale' => $fbPersonProfile['locale'], 'picture' => $fbPersonProfile['picture']['url'], 'link' => $fbPersonProfile['link'] ); $personData = $user->checkPerson($fbPersonData); $_SESSION['personData'] = $personData; $form_logoutURL = $helper->getLogoutUrl($getaccesstoken, $redirectURL.'form_logout.php'); if(!empty($personData)){ $data_output = '<h1>Facebook Profile Details </h1>'; $data_output .= '<img src="'.$personData['picture'].'">'; $data_output .= '<br/>Facebook ID : ' . $personData['oauth_uid']; $data_output .= '<br/>Name : ' . $personData['per_fname'].' '.$personData['per_lname']; $data_output .= '<br/>Email : ' . $personData['email']; $data_output .= '<br/>Gender : ' . $personData['gender']; $data_output .= '<br/>Locale : ' . $personData['locale']; $data_output .= '<br/>Logged in with : Facebook'; $data_output .= '<br/><a href="'.$personData['link'].'" target="_blank">Click to Visit Facebook Page</a>'; $data_output .= '<br/>Logout from <a href="'.$form_logoutURL.'">Facebook</a>'; }else{ $data_output = '<h3 style="color:red">Some problem occurred, please try again.</h3>'; } }else{ $loginURL = $helper->getLoginUrl($redirectURL, $fbPermissions); $data_output = '<a href="'.htmlspecialchars($loginURL).'"><img src="images/fblogin-btn.png"></a>'; } ?> <html> <head> <title>Simple Login with Facebook using PHP by live24u Steps</title> <style type="text/css"> h1{font-family:Arial, Helvetica, sans-serif;color:#999999;} </style> </head> <body> <div><?php echo $data_output; ?></div> </body> </html>
Step 6 : Logout (form_logout.php)
<?php require_once 'fbConfig.php'; unset($_SESSION['get_fb_access_token']); unset($_SESSION['personData']); header("Location:index.php"); ?>
I hope you have Got What is Simple Login with Facebook Connect PHP Source Code Download And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.