Facebook.php (Save to application/config folder)
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
$config['appId'] = 'Your App ID';
$config['secret'] = 'Your App Secret';
?>
Welcome.php (Save to application/controllers folder)
<?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' => 'APP_ID',
// 'secret' => 'SECRET',
// ));
$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
// OR
// Logs off FB!
// $data['logout_url'] = $this->facebook->getLogoutUrl();
} else {
$data['login_url'] = $this->facebook->getLoginUrl(array(
'redirect_uri' => site_url('welcome/login'),
'scope' => array("email") // permissions here
));
}
$this->load->view('login',$data);
}
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');
}
}
login.php (Save to application/views folder)
<html>
<head>
<title>Login with Facebook Using CodeIgniter</title>
</head>
<body>
<div class="container">
<form>
<!-- @user_profile check when user login successed -->
<?php if (@$user_profile): // call var_dump($user_profile) to view all data ?>
<!-- Display profile photo -->
<img src="https://graph.facebook.com/<?=$user_profile['id']?>/picture?type=large" style="width: 140px; height: 140px;">
<!-- Display profile name -->
<h2><?=$user_profile['name']?></h2>
<!-- Create link to facebook profile -->
<a href="<?=$user_profile['link']?>">View Profile</a>
<!-- Create link logout -->
<a href="<?= $logout_url ?>">Logout</a>
<!-- Display login when start home page first -->
<?php else: ?>
<h2>Login with Facebook Using CodeIgniter</h2>
<a href="<?= $login_url ?>">Login</a>
<?php endif; ?>
</form>
</div>
</body>
</html>
libraries download from demo and then extract to application/libraries folder
http://downloads.ziddu.com/download/24375128/cfblogin.zip.html
Read more blog
http://www.freeonlinelecture.com
Follow Us with Facebook
https://www.facebook.com/pages/PHP-Tutorial/782239958529506
Follow Us with Facebook
https://www.facebook.com/pages/PHP-Tutorial/782239958529506

2 comments
commentsA PHP Error was encountered
ReplySeverity: Notice
Message: Undefined index: link
Filename: views/login.php
Line Number: 18
">View Profile Logout
Warning: include(/home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/app/views/errors/html/error_php.php): failed to open stream: No such file or directory in /home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/ci_3.0.0/core/Exceptions.php on line 243
ReplyWarning: include(): Failed opening '/home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/app/views/errors/html/error_php.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/ci_3.0.0/core/Exceptions.php on line 243
Warning: include(/home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/app/views/errors/html/error_php.php): failed to open stream: No such file or directory in /home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/ci_3.0.0/core/Exceptions.php on line 243
Warning: include(): Failed opening '/home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/app/views/errors/html/error_php.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/ci_3.0.0/core/Exceptions.php on line 243