PHP script for Login with Facebook and Twitter

Posted on October 3, 2011

0


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 int(11),
oauth_provider VARCHAR(100),
username VARCHAR(100),
twitter_oauth_token VARCHAR(200),
twitter_oauth_token_secret VARCHAR(200)
);
The tutorial contains three folders called facebook,twitter and config with PHP files.
facebook //Facebook OAUTH library
twitter //Twitter OAUTH library
config
— functions.php
— dbconfig.php //Database connection
— fbconfig.php //Facebook API connection
— twconfig.php //Twitter API connection
index.php
home.php
login-twitter.php
login-facebook.php
getTwitterData.php
Facebook Setup
You have to create a application. Facebook will provide you app id and app secret id, just modify following code
fcconfig.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’, ‘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());
?>
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&#8217;);
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.
Advertisements
Posted in: PHP