X
    Categories: FacebookMysqlPHP

Login with Facebook and Twitter

Facebook and Twitter have become large in the social network world and both networks offering oAuth support. We developed a system to login with Twitter and Facebook. Nowadays web users not interested to filling the big registration forms. This script helps you to avoid registration forms, It’s is very useful and simple to integrate.

Database
Sample database users table columns id, email, oauth_uid, oauth_provider and username.

CREATE TABLE users
(
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) 
);

Facebook Setup

You have to create a application. Facebook will provide you app id and app secret id, just modify following code

fgconfig.php

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

Twitter Setup

Create a twitter application click here. Some like Facebook Twitter provide you consumer key amd consumer secret key using these modify following code.


twconfig.php

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

dbconfig.php
Database configuration file. 

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

login-twitter.php
In root directory find out the below line at login-twitter.php code and replace yourwebsite.

$request_token = $twitteroauth->getRequestToken('http://yourwebsite.com/getTwitterData.php');

index.php
If you want to modify your web project existing login or index pages, just use following code.  

<?php
session_start();
if (isset($_SESSION['id'])) {
// Redirection to login page twitter or facebook
header("location: home.php");
}
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>

If any queries please comment here.

 

Marimuthu: