Learn how to do Facebook Login In Codigneiter

Facebook Login in Codigneiter

learn how to do Facebook Login In Codigneiter in this tutorial. An easy way to do this is proceeded in the tutorial. All version are available here.

Step wise Doing Facebook integration in codeigniter so follow the steps

Step-1 Download this link Codigneiter setup https://ellislab.com/codeigniter/user-guide/installation/downloads.html

all version are available in this link, then set the setup.

Step-2 Then cone config/config.php.

config

config.php

 

Step-3 Open this link https://developers.facebook.com

 

app1

 

app2

Step-4

 

app3

app4

Step-5  Copy app Id & secret Key then config/Facebook.php

config_fb

Step-6 Download the Library https://github.com/bugzbrown/codeigniter-facebook-lib

Step-7 Unzip to Folder then copy All Files and Past to “libraries Folder”

Step-8 controllar/welcome.php

<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

class Welcome extends CI_Controller {

public function __construct(){
parent::__construct();

// To use site_url and redirect on this controller.
$this->load->helper(‘url’);
}

public function login(){

$this->load->library(‘facebook’); // Automatically picks appId and secret from config
// OR
// You can pass different one like this
$this->load->library(‘facebook’, array(
‘appId’ => ‘appid’,
‘secret’ => ‘appsecret’,
));$user = $this->facebook->getUser();
if ($user) {
try {
$data[‘user_profile’] = $this->facebook->api(‘/me’);
} catch (FacebookApiException $e) {
$user = null;
}
}else {
$this->facebook->destroySession();
}if ($user) {$data[‘logout_url’] = site_url(‘welcome/logout’); // Logs off application
} else {
$data[‘login_url’] = $this->facebook->getLoginUrl(array(
‘redirect_uri’ => site_url(‘welcome/login’),
‘scope’ => array(“email”) // permissions here
));
}
$this->load->view(‘login’,$data);}
function remSess()
{ $data[‘logoutUrl’] = $this->facebook->getLogoutUrl();
$data2 = array(‘user_id’ => ”,’logged_in’ => ”);
$this->session->unset_userdata($data2);
$this->session->sess_destroy();
session_destroy();
$data = array(‘sess’ => “gone” );
echo json_encode($data[“sess”]=”gone”);
}
public function logout(){
$this->load->library(‘facebook’);

// Logs off session from website
$this->facebook->destroySession();
// Make sure you destory website session as well

redirect(‘welcome/login’);
}

}

Step-9 View/login

<HTML>
<head>
<title>Login with Facebook</title>
<link rel=”stylesheet” href=”//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css”>
<link rel=”stylesheet” href=”//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css”>
<style type=”text/css”>
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #eee;
}

.form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;
}
.form-signin .form-signin-heading{
padding-bottom: 10px;
margin-bottom: 20px;
border-bottom: 1px #ccc dotted;
text-align: center;
}
.form-signin .footer{
padding-top: 10px;
margin-top: 20px;
border-top: 1px #ccc dotted;
font-weight: 600;
}
.fa {
color: #cc0000;
}
</style>
</head>
<body>

<div class=”container” >
<form class=”form-signin” role=”form”>
<?php if (@$user_profile):  // call var_dump($user_profile) to view all data ?>
<div class=”row”>
<div class=”col-lg-12 text-center”>
<table border=”3″ align=”center”>
<tr>
<td>First Name  </td>
<td><?= $user_profile[‘first_name’] ?></td>
</tr>
<tr>
<td>Last Name  </td>
<td><?= $user_profile[‘last_name’] ?></td>
</tr>
<tr>
<td>Gender  </td>
<td><?= $user_profile[‘gender’] ?></td>
</tr>
<tr>
<td>Hometown  </td>
<td><?=
$user_profile[‘hometown’][‘name’];
?></td>
</tr>
<tr>
<td>Timezone  </td>
<td><?= $user_profile[‘timezone’] ?></td>
</tr>
<tr>
<td>Updated_time  </td>
<td><?= $user_profile[‘updated_time’] ?></td>
</tr>
<tr>
<td>currently  </td>
<td><?= $user_profile[‘work’][‘1′][‘location’][‘name’] ?></td>
</tr>
</table>
<br><br>

<img class=”img-thumbnail” data-src=”holder.js/140×140″ alt=”140×140″ src=”https://graph.facebook.com/<?=   $user_profile[‘id’] ?>/picture?type=large” style=”width: 140px; height: 140px;”>
<h2><?= $user_profile[‘name’] ?></h2>
<a href=”<?= $user_profile[‘link’] ?>” class=”btn btn-lg btn-default btn-block” role=”button”>View Profile</a>
<?php $logout = base_url() . “index.php/welcome/logout”; ?>
<a href=”<?= $logout; ?>” class=”btn btn-lg btn-primary btn-block” onclick=”RemoveSess()” role=”button”>Logout</a>

<?php else: ?>
<h2 class=”form-signin-heading”>Login with Facebook</h2>
<a href=”<?= $login_url ?>” class=”btn btn-lg btn-primary btn-block” role=”button”>Login</a>
<?php endif; ?>
</form>
</div> <!– /container –>
<script type=”text/javascript” src=”//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js”></script>
</body>
</html>

 

Step-10  Output

output-1output-final

THANK YOU FRIENDS