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)
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)
$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)
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 = 'Facebook Profile Details
'; $data_output .= ''; $data_output .= '
Facebook ID : ' . $personData['oauth_uid']; $data_output .= '
Name : ' . $personData['per_fname'].' '.$personData['per_lname']; $data_output .= '
Email : ' . $personData['email']; $data_output .= '
Gender : ' . $personData['gender']; $data_output .= '
Locale : ' . $personData['locale']; $data_output .= '
Logged in with : Facebook'; $data_output .= '
Click to Visit Facebook Page'; $data_output .= '
Logout from Facebook'; }else{ $data_output = 'Some problem occurred, please try again.
'; } }else{ $loginURL = $helper->getLoginUrl($redirectURL, $fbPermissions); $data_output = ''; } ?>Simple Login with Facebook using PHP by live24u Steps
Step 6 : Logout (form_logout.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.