PHP CodeIgniter Login with Facebook Step By step
In this Post We Will Explain About is PHP CodeIgniter Login with Facebook Step By step 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 in CodeigniterExample
In this post we will show you Best way to implement Login with Facebook and Twitter using CodeIgniter, hear for CodeIgniter Login With Facebook Twitter and Google using PHPwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.
explations Facebook user Login is the most used feature for now presents url web application. Login of Facebook feature helps clients for log into the web application easily. Which means it’s your web application receives more clients / customers. we have already published Login of facebook using PHP it’s shell help We for implement Facebook user Login in PHP. In this tutorial, we Have to going for explain how for explations Facebook user Login in PHP CodeIgniter using Facebook PHP SDK of Facebook Graph API.
Before We begin for implement Facebook user Login in PHP CodeIgniter 3 using SDK v5, make a Facebook web-app in Facebook developers panel as well as get the web-application ID as well as web-application Secret. We should need for set Site URL it’s would be the same of client authentication controller URL Like as a (http://localhost/codeigniter/client_authentication/). Also need for specify the Redirect URL, web-application ID, as well as web-application Secret in your script at the time of connecting of Facebook Graph API.
How for MAke Facebook web-application, web-application ID, as well as web-application Secret
Once your Facebook web-app creation is completed, as well as copy the web-application ID as well as web-application Secret as well as put them into the respective variable in your script.
Take a look on files structure of Login of Facebook in PHP CodeIgniter using PHP SDK v5.
Database Table Creation
To store the Facebook profile information, We need for make a table into the database. The source code the following SQL creates a clients table of some required fields in MySQL database.
//devloped by Pakainfo.com CREATE TABLE `clients` ( `id` int(11) NOT NULL AUTO_INCREMENT, `oauth_provider` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `oauth_uid` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `fname` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `lname` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `gender` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `locale` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `profile_img` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `client_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `cr_date` datetime NOT NULL, `modified` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Config
autoload.php
The database as well as session libraries are needed, it shell better for specify these libraries in the autoload.php file.
//devloped by Pakainfo.com $autoload['libraries'] = array('session','database');
facebook.php
Facebook web-application as well as API configuration variables are defined in this file. We need for specify the web-application Id, web-application Secret as well as redirect URLs according for your Facebook web-application credentials.
Client_Authentication controller contains three functions, __construct(), index(), as well as logout().
__construct() – The Facebook library as well as Client model are loaded in this method.
index() – The source code the following functionalities are implemented in this method.
Connect of Facebook Graph API using the Facebook library, pass the client profile information for the Client model for insert into the database.
Pass the client data for the view as well as load the profile details view for authenticated client.
Load the user Login view for the non-authenticated client.
simple logout() – This method destroys the Facebook session, remove the client data from session as well as logout the client from their Facebook account.//devloped by Pakainfo.com load->library('facebook'); $this->load->model('client'); } //devloped by Pakainfo.com public function index(){ $clientData = array(); //devloped by Pakainfo.com if($this->facebook->is_authenticated()){ $clientProfile = $this->facebook->request('get', '/me?fields=id,fname,lname,email,gender,locale,picture'); $clientData['oauth_provider'] = 'facebook'; $clientData['oauth_uid'] = $clientProfile['id']; $clientData['fname'] = $clientProfile['fname']; $clientData['lname'] = $clientProfile['lname']; $clientData['email'] = $clientProfile['email']; $clientData['gender'] = $clientProfile['gender']; $clientData['locale'] = $clientProfile['locale']; $clientData['client_url'] = 'https://www.facebook.com/'.$clientProfile['id']; $clientData['profile_img'] = $clientProfile['picture']['data']['url']; $clientID = $this->client->clientChek($clientData); if(!empty($clientID)){ $data['clientData'] = $clientData; $this->session->set_clientdata('clientData',$clientData); }else{ $data['clientData'] = array(); } $data['userlogout'] = $this->facebook->logout_url(); }else{ $fbclient = ''; $data['url_auth'] = $this->facebook->login_url(); } $this->load->view('client_authentication/index',$data); } //devloped by Pakainfo.com public function logout() { $this->facebook->destroy_session(); //devloped by Pakainfo.com $this->session->unset_clientdata('clientData'); redirect('/client_authentication'); } }Libraries
Facebook.php
The Facebook class helps for explations Facebook PHP SDK v5 in PHP CodeIgniter 3.x application. Using this Facebook library, We can easily add the user Login of Facebook functionality using PHP SDK v5 for the PHP CodeIgniter application.
//devloped by Pakainfo.com load->config('facebook'); $this->load->library('session'); $this->load->helper('url'); if (!isset($this->fb)){ $this->fb = new FB([ 'app_id' => $this->config->item('facebook_app_id'), 'app_secret' => $this->config->item('facebook_app_secret'), 'default_graph_version' => $this->config->item('facebook_graph_version') ]); } //devloped by Pakainfo.com switch ($this->config->item('facebook_login_type')){ case 'js': $this->helper = $this->fb->getJavaScriptHelper(); break; case 'canvas': $this->helper = $this->fb->getCanvasHelper(); break; case 'page_tab': $this->helper = $this->fb->getPageTabHelper(); break; case 'web': $this->helper = $this->fb->getRedirectLoginHelper(); break; } if ($this->config->item('facebook_auth_on_load') === TRUE){ $this->authenticate(); } } //devloped by Pakainfo.com public function object(){ return $this->fb; } //devloped by Pakainfo.com public function is_authenticated(){ $get_access_token = $this->authenticate(); if(isset($get_access_token)){ return $get_access_token; } return false; } //devloped by Pakainfo.com public function request($method, $endpoint, $params = [], $get_access_token = null){ try{ $response = $this->fb->{strtolower($method)}($endpoint, $params, $get_access_token); return $response->getDecodedBody(); }catch(FacebookResponseException $e){ return $this->logError($e->getCode(), $e->getMessage()); }catch (FacebookSDKException $e){ return $this->logError($e->getCode(), $e->getMessage()); } } //devloped by Pakainfo.com public function login_url(){ if($this->config->item('facebook_login_type') != 'web'){ return ''; } //devloped by Pakainfo.com return $this->helper->getLoginUrl( base_url() . $this->config->item('facebook_login_redirect_url'), $this->config->item('facebook_permissions') ); } //devloped by Pakainfo.com public function logout_url(){ if($this->config->item('facebook_login_type') != 'web'){ return ''; } return $this->helper->getLogoutUrl( $this->get_access_token(), base_url() . $this->config->item('facebook_logout_redirect_url') ); } //devloped by Pakainfo.com public function destroy_session(){ $this->session->unset_clientdata('fb_access_token'); } //devloped by Pakainfo.com private function authenticate(){ $get_access_token = $this->get_access_token(); if($get_access_token && $this->get_expire_time() > (time() + 30) || $get_access_token && !$this->get_expire_time()){ $this->fb->setDefaultAccessToken($get_access_token); return $get_access_token; } //devloped by Pakainfo.com if(!$get_access_token){ try{ $get_access_token = $this->helper->getAccessToken(); }catch (FacebookSDKException $e){ $this->logError($e->getCode(), $e->getMessage()); return null; } if(isset($get_access_token)){ $get_access_token = $this->long_lived_token($get_access_token); $this->set_expire_time($get_access_token->getExpiresAt()); $this->set_access_token($get_access_token); $this->fb->setDefaultAccessToken($get_access_token); return $get_access_token; } } //devloped by Pakainfo.com if($this->config->item('facebook_login_type') === 'web'){ if($this->helper->getError()){ $error = array( 'error' => $this->helper->getError(), 'error_code' => $this->helper->getErrorCode(), 'error_reason' => $this->helper->getErrorReason(), 'error_description' => $this->helper->getErrorDescription() ); return $error; } } return $get_access_token; } //devloped by Pakainfo.com private function long_lived_token(AccessToken $get_access_token){ if(!$get_access_token->isLongLived()){ $oauth2_client = $this->fb->getOAuth2Client(); try{ return $oauth2_client->getLongLivedAccessToken($get_access_token); }catch (FacebookSDKException $e){ $this->logError($e->getCode(), $e->getMessage()); return null; } } return $get_access_token; } //devloped by Pakainfo.com private function get_access_token(){ return $this->session->clientdata('fb_access_token'); } //devloped by Pakainfo.com private function set_access_token(AccessToken $get_access_token){ $this->session->set_clientdata('fb_access_token', $get_access_token->getValue()); } //devloped by Pakainfo.com private function get_expire_time(){ return $this->session->clientdata('fb_expire'); } //devloped by Pakainfo.com private function set_expire_time(DateTime $time = null){ if ($time) { $this->session->set_clientdata('fb_expire', $time->getTimestamp()); } } //devloped by Pakainfo.com private function logError($code, $message){ log_message('error', '[FACEBOOK PHP SDK] code: ' . $code.' | message: '.$message); return ['error' => $code, 'message' => $message]; } //devloped by Pakainfo.com public function __get($var){ return get_instance()->$var; } }facebook-php-sdk/
simple Facebook PHP SDK is used for data connect of the Facebook any Graph API as well as simple explations user Login system of Facebook according for authenticate. The simple facebook-php-sdk/ directory or folder contains the simple libs like as a latest version (v5) of Facebook download SDK for PHP.
Models (Client.php)
Client model contains one simple function called as a clientChek(), it is most used for insert or update data the client profile some information into the mysql database.
//devloped by Pakainfo.com tableName = 'clients'; $this->primaryKey = 'id'; } public function clientChek($data = array()){ $this->db->select($this->primaryKey); $this->db->from($this->tableName); //devloped by Pakainfo.com $this->db->where(array('oauth_provider'=>$data['oauth_provider'],'oauth_uid'=>$data['oauth_uid'])); $sql_prev = $this->db->get(); $prevCheck = $sql_prev->num_rows(); if($prevCheck > 0){ $resultPrev = $sql_prev->row_array(); $data['modified'] = date("Y-m-d H:i:s"); $update = $this->db->update($this->tableName,$data,array('id'=>$resultPrev['id'])); $clientID = $resultPrev['id']; }else{ $data['cr_date'] = date("Y-m-d H:i:s"); $data['modified'] = date("Y-m-d H:i:s"); $insert = $this->db->insert($this->tableName,$data); $clientID = $this->db->insert_id(); } return $clientID?$clientID:FALSE; } }Views (client_authentication/index.php)
If the client already any logged in of their Facebook account(Social accounts), this view shell display all the data the profile details, otherwise, simple username or password for Facebook user Login button shell be shown.
//devloped by Pakainfo.com'; }else{ ?>
Facebook Profile Details
Welcome
Clients Facebook ID :
Clients Name :
Clients Email :
Clients Gender :
Clients Locale :
We are user Login of : Facebook
Logout from Facebook
I hope you have Got What is how to integrate twitter login and facebook login in a web site using CodeIgniter 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.