Login with Facebook and Twitter

Facbook and Twitter have very popular social network in world that give oAuth support for login. You can give authentication for a user to login with Facebook and Twitter account. For set up of Facebook and Twitter account login you can need to add some script to your account.

Login with Facbook and Twitter

Login with Facbook and Twitter

Now in days user not interested for fill form and user just sign in at one click so Facbook and Twitter give authentication for site to login an account and create a profile from their social media account. This script helps to use simple scripts for registration form and login form.

We give you to scripts to set up your login with facebook and Twitter. The step by step guide of login give at below please follow this steps, its very simple to create login with Facebook and Twitter.

Designing your Database

Design your database with below code:

CREATE TABLE members
(
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(70),
oauth_uid VARCHAR(200),
oauth_provider VARCHAR(200),
username VARCHAR(100),
twitter_oauth_token VARCHAR(200),
twitter_oauth_token_secret VARCHAR(200)
);

 

dbconfig.php

Create your database file and insert your database detail.

<?php
define(‘DB_SERVER’, ‘localhost’);
define(‘DB_USERNAME’, ‘User Name’);
define(‘DB_PASSWORD’, ‘Password’);
define(‘DB_DATABASE’, ‘DATABASE’);
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database =  mysql_select_db(DB_DATABASE) or die (mysql_error());
?>

functions.php

Use this file that functions used in the script.

<?php

require ‘dbconfig.php’;

class User {

function checkUser($uid, $oauth_provider, $username,$email,$twitter_otoken,$twitter_otoken_secret)

{

$query = mysql_query(“SELECT * FROM `members` WHERE oauth_uid = ‘$uid’ and oauth_provider = ‘$oauth_provider'”) or die(mysql_error());

$result = mysql_fetch_array($query);

if (!empty($result)) { //Attention! here it is empty() only once not emptyempty. This is a bug in the code highlighter

// User is already present

} else {

//user not present

$query = mysql_query(“INSERT INTO `members` (oauth_provider, oauth_uid, username,email,twitter_oauth_token,twitter_oauth_token_secret) VALUES (‘$oauth_provider’, $uid, ‘$username’,’$email’)”) or die(mysql_error());

$query = mysql_query(“SELECT * FROM `members` WHERE oauth_uid = ‘$uid’ and oauth_provider = ‘$oauth_provider'”);

$result = mysql_fetch_array($query);

return $result;

}

return $result;

}

}

?>

Facebook Setup

Now set up your Facbook account with below code

fbconfig.php

Create fb configure file with this code:

<?php
define(‘APP_ID’, ‘Facebook APP ID’);
define(‘APP_SECRET’, ‘Facebook Secret ID’);
?>

login-fb.php

This code for login file:

<?php

require ‘facebook/facebook.php’;

require ‘config/fbconfig.php’;

require ‘config/functions.php’;

$facebook = new Facebook(array(

‘appId’ => APP_ID,

‘secret’ => APP_SECRET,

));

$user = $facebook->getUser();

if ($user) {

try {

// If the user is authenticated and logged-in

$user_profile = $facebook->api(‘/me’);

} catch (FacebookApiException $e) {

error_log($e);

$user = null;

}

if (!empty($user_profile )) {

$username = $user_profile[‘name’];

$uid = $user_profile[‘id’];

$email = $user_profile[’email’];

$user = new User();

$userdata = $user->checkUser($uid, ‘facebook’, $username,$email,$twitter_otoken,$twitter_otoken_secret);

if(!empty($userdata)){

session_start();

$_SESSION[‘id’] = $userdata[‘id’];

$_SESSION[‘oauth_id’] = $uid;

$_SESSION[‘username’] = $userdata[‘username’];

$_SESSION[’email’] = $email;

$_SESSION[‘oauth_provider’] = $userdata[‘oauth_provider’];

header(“Location: home.php”);

}

} else {

die(“There was an error.”);

}

}

else {

// Generate a session if there is none.

$login_url = $facebook->getLoginUrl(array( ‘scope’ => ’email’));

header(“Location: ” . $login_url);

}

?>

Twitter Setup

Now set up up your twitter login form:

twconfig.php

Create configure file with code:

<?php
define(‘YOUR_CONSUMER_KEY’, ‘Twitter Key’);
define(‘YOUR_CONSUMER_SECRET’, ‘Twitter Secret Key’);
?>

login-tw.php

Create login file with code:

<?php

require(“twitter/twitteroauth.php”);

require ‘config/twconfig.php’;

session_start();

$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);

// Requesting authentication tokens, the parameter is the URL we will be redirected to

$request_token = $twitteroauth->getRequestToken(‘http://yourwebsite.com/getTwitterData.php’); //remember to replace your site here

// Saving them into the session

$_SESSION[‘oauth_token’] = $request_token[‘oauth_token’];

$_SESSION[‘oauth_token_secret’] = $request_token[‘oauth_token_secret’];

// If everything is OK

if ($twitteroauth->http_code == 200) {

// generating the URL and redirect

$url = $twitteroauth->getAuthorizeURL($request_token[‘oauth_token’]);

header(‘Location: ‘ . $url);

} else {

die(‘Something wrong happened.’);

}

?>

index.php

<?php
session_start();
if (isset ($_SESSION[‘id’])) {
// Redirection to login page twitter or facebook
header<span>(“</span><span>location: home.php</span><span>”);</span>
}
if (array_key_exists(“login”, $_GET))
{
$oauth_provider = $_GET[‘oauth_provider’];
if ($oauth_provider == ‘twitter’)
{
header(“Location: login-twitter.php”);
}
else if ($oauth_provider == ‘facebook’)
{
header(“Location: login-facebook.php”);
}
}
?>
//HTML Code
<a href=”?login&oauth_provider=twitter”>Twitter_Login</a>
<a href=”?login&oauth_provider=facebook”>Facebook_Login</a>

home.php

<?php

//Always place this code at the top of the Page

session_start();

if (!isset($_SESSION[‘id’])) {

// Redirection to login page twitter or facebook

header(“location: index.php”);

}
echo ‘ Welcome’;  // or whatever you want your user to see.

?>

logout.php

<?php

if (array_key_exists(“logout”, $_GET)) {

session_start();

unset($_SESSION[‘id’]);

unset($_SESSION[‘username’]);

unset($_SESSION[‘oauth_provider’]);

session_destroy();

header(“location: home.php”);

}

?>

For this code you can set logout section.

Login with Facbook and Twitter

Login with Facbook and Twitter

This tutorial for set up your Facebook and Twitter login form with help of this code. If you have face any problem than contact with us and give us your feedback at below.

If you update with us like this tutorial than sign up with us at our newsletter at below with just enter your email and confirm your email .

Powered By DT Author Box

Written by Cloudy Cip

2 comments

  1. Hello. Very nice website!! Man .. Excellent .. Amazing .. I will bookmark your website and take the feeds also…I am happy to locate numerous helpful info right here within the post. Thank you for sharing.

  2. Great website. A lot of useful information here. I’m sending
    it to several pals ans also sharing in delicious.
    And obviously, thanks for your sweat!

Leave a Reply

Your email address will not be published. Required fields are marked *

banner
Read previous post:
Samsung Galaxy S Advance Price In India

Samsung Galaxy is most popular mobile in India and Samsung Galaxy launched Samsung Galaxy advance which is available in India...

Close