Facebook login in Codeigniter
Hi, guys. Today topic is FaceBook loging system in PHP Codeigniter Framewoek. If you have a website and using programming PHP Codeigniter framework then this tutorial will be most helpful for you to give a user sign up with FaceBook. Sing up and login with facebook using FaceBook API and FaceBook libraries.
Create a Facebook App
First you need to create a Facebook App for it and collect these option.
- Facebook APP Id
- Facebook App Secret
- Facebook login redirect URL
Click here to create Facebook App. Follow the below picture, it help you.
Create MySQL table
Now open your MySQL database and create a table whare user data will be inserted. Follow the below instruction and create a table with these field.
CREATE TABLE `user_logins` ( `register_id` int(11) NOT NULL AUTO_INCREMENT, `oauth_provider` enum('','facebook') COLLATE utf8_unicode_ci NOT NULL, `oauth_uid` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `username` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `user_email` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `gender` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `picture_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `register_date` datetime NOT NULL, `last_update_date` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Use FaceBook library
Put FaceBook libraries file in your project folder. Download Facebook Library and paste into the Library folder. For example:
- Project Folder
- application
- libraries
- libraries
- application
Create Facebook.php page
You need to create facebook.php page into the config folder and set Facebook APP Id, Facebook App Secret, Facebook login redirect URL. I have also example code, you can copy that and paste it. After get these example code change Facebook APP Id, Facebook App Secret, Facebook login redirect URL and now move.
- Project Folder
- application
- config
- facebook.php
- config
- application
<?php defined('BASEPATH') OR exit('No direct script access allowed'); $config['facebook_app_id'] = 'Your Facebook app ID'; $config['facebook_app_secret'] = 'Your Facebook app secret '; $config['facebook_login_type'] = 'web'; $config['facebook_login_redirect_url'] = 'facebook-signup'; $config['facebook_logout_redirect_url'] = 'https://example.in/mtp-tw/logout'; $config['facebook_permissions'] = array('public_profile', 'publish_actions', 'email'); $config['facebook_graph_version'] = 'v2.6'; $config['facebook_auth_on_load'] = TRUE;
Generate a FaceBook sign up link
Create a Facebook link on your web page. When user click on link then user data insert into the database through FaceBook.
<p> <a href="<?php echo base_url(); ?>facebook-signup" class="facebook_btn"> <span class="fa fa-facebook-f"></span> Sign in With <strong>Facebook</strong> </a> </p>
Create Route
If you are using route then open the route page and create a route. Define same route name which routr name you are put in the anchor tag. For example:
<?php $route['facebook-signup'] = 'LoginController/facebook_signup'; ?>
Create Controller like LoginController
Create a Controller and give controller name LoginController and follow the example code and try with these example code.
<?php class LoginController extends CI_controller{ public function __construct(){ parent::__construct(); // Helper $this->load->helper(array('form', 'url')); // Library $this->load->library('session'); // Model $this->load->model('LoginModel'); } // Facebook Signup & Login public function facebook_signup(){ $this->load->library('facebook'); $userData = array(); // Check if user is logged in if($this->facebook->is_authenticated()){ // Get user facebook profile details $fbUserProfile = $this->facebook->request('get', '/me?fields=id,first_name,last_name,email,link,gender,picture'); // Preparing data for database insertion $userData['oauth_provider'] = 'facebook'; $userData['oauth_uid'] = $fbUserProfile['id']; // Insert user first name and last name into username $userData['username'] = $fbUserProfile['first_name']." ".$fbUserProfile['last_name']; $userData['user_email'] = $fbUserProfile['email']; // check gender value is exist or not if($fbUserProfile['gender']=="male"){ $gender = 'M'; } elseif($fbUserProfile['gender']=="female"){ $gender = 'F'; } elseif($fbUserProfile['gender']=="other"){ $gender = 'O'; } else{ $gender = ''; } $userData['gender'] = $gender; $userData['picture_url'] = $fbUserProfile['picture']['data']['url']; // Insert or update user data $userID = $this->LoginModel->facebookSignup($userData); // Check user data insert or update status if(!empty($userID)){ $this->session->set_userdata('user_id',$userID); redirect('dashboard'); } }else{ // Get login URL $authURL = $this->facebook->login_url(); redirect($authURL); } } } ?>
Create Model
Example code is here:
<?php class LoginModel extends CI_Model{ function __construct() { parent::__construct(); // Database $this->load->database(); } public function facebookSignup($data = array()){ $this->db->select('*'); $this->db->from('register'); $this->db->where(array('oauth_provider'=>$data['oauth_provider'],'oauth_uid'=>$data['oauth_uid'])); $query = $this->db->get(); $check = $query->num_rows(); if($check > 0){ $result = $query->row_array(); //$data['last_update_date'] = date("Y-m-d"); $update = $this->db->update('register',['last_update_date'=>date("Y-m-d H:i:s")],array('register_id'=>$result['register_id'])); $userID = $result['register_id']; }else{ $data['pen'] = $pen; $data['register_date'] = date("Y-m-d H:i:s"); $data['last_update_date']= date("Y-m-d H:i:s"); $data['register_status'] = "A"; $insert = $this->db->insert('register',$data); $userID = $this->db->insert_id(); } return $userID?$userID:false; } } ?>
Thank you. A good website that provides good information like this that makes new knowledge even more good.