Facebook login with our websites using codeigniter
It should present your controller: initially you should include the two files like facebook.php and base_facebook.php you can get this two libraries from following website..
and
Controller:
controller name: home.php
<?php
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
// $this->load->library('facebook');
$fb_config = array(
'appId' => '265991286842551',
'secret' => '****************************'
);
$this->load->library('facebook', $fb_config);
$user = $this->facebook->getUser();
if ($user) {
try {
$data['user_profile'] = $this->facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
$this->load->model('home_model');
$this->home_model->insert();
}
if ($user) {
$data['logout_url'] = $this->facebook->getLogoutUrl();
} else {
$data['login_url'] = $this->facebook->getLoginUrl();
}
$this->load->view('view',$data);
}
}
model:
home_model.php
<?php
class Home_model extends CI_Model {
function insert(){
$user_profile = $this->facebook->api('/me');
$fbid=$user_profile['id'];
$this->db->select('fbid');
$this->db->where('fbid', $fbid);
$query=$this->db->get('user');
$num=$query->num_rows();
if($num){
return false;
}
else{
$insert1=array (
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
'fbemail' => $user_profile['username'],
'current_location' => $user_profile['location']['name'],
'fbid' => $user_profile['id']
);
$this->db->insert('user' ,$insert1);
return true;
}
}
}
view :
<html>
<head>
<title>Facebook Sweetness</title>
</head>
<body>
<h1>Facebook stuff</h1>
<?php if (@$user_profile): ?>
<?php $user_profile['name'];?>
<pre>
<?php // echo print_r($user_profile, TRUE) ?>
</pre>
<a href="<?php echo $logout_url ?>">Logout of this thing</a>
<?php else: ?>
<h2>Welcome to this facebook thing, please login below</h2>
<a href="<?php echo $login_url ?>">Login to this thing</a>
<?php endif; ?>
</body>
</html>
For logout url:
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => 'Your Redirect URL after logout', // URL to which to redirect the user after logging out ));
For login url:
if($user){ // Get logout URL $logoutUrl = $facebook->getLogoutUrl(); }else{ // Get login URL $loginUrl = $facebook->getLoginUrl(array( 'scope' => 'read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos', )); }
from here we can get userbirthday and location user work history hometown , photos everything...
For getting profile picture:
echo "</br><img src='https://graph.facebook.com/$user/picture?type=large'/>";
For getting email:
$loginUrl = $facebook->getLoginUrl(array( 'scope' => 'email' ));
No comments:
Post a Comment