Twitter Login Script
no matter how you know about this.
this is a simple script just embed this within your site and you got the result output that twiiter login works user friendly.
Let's Start the File Structure
We use Three Files For twitter Login
- index.php
- home.php
- twitteroauth.php
- OAuth.php
- twconfig.php
- login-twitter.php
- getTwitterData.php
- functions.php
Download the twitteroauth.php & OAuth.php File
Let's Start the Coding Part
1. index.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: home.php");
}
if (array_key_exists("login", $_GET)) {
$oauth_provider = $_GET['oauth_provider'];
if ($oauth_provider == 'twitter') {
header("Location: login-twitter.php");
}
}
?>
<title>onlinewebapplication.com Facebook | Twitter Login</title>
<style type="text/css">
#buttons
{
text-align:center
}
#buttons img,
#buttons a img
{ border: none;}
h1
{
font-family:Arial, Helvetica, sans-serif;
color:#999999;
}
</style>
<div id="buttons">
<h1>Twitter Facebook Login </h1>
<a href="?login&oauth_provider=twitter"><img src="images/tw_login.png"></a>
</div>
2. 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 '<h1>Welcome</h1>';
echo 'id : ' . $_SESSION['id'];
echo '<br/>Name : ' . $_SESSION['username'];
echo '<br/>Email : ' . $_SESSION['email'];
echo '<br/>You are login with : ' . $_SESSION['oauth_provider'];
echo '<br/>Logout from <a href="logout.php?logout">' . $_SESSION['oauth_provider'] . '</a>';
?>
3. twconfig.php
<?php
define('YOUR_CONSUMER_KEY', 'Your Twitter App key');
?>
4. login-twitter.php
Let's Start the Coding Part
1. index.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: home.php");
}
if (array_key_exists("login", $_GET)) {
$oauth_provider = $_GET['oauth_provider'];
if ($oauth_provider == 'twitter') {
header("Location: login-twitter.php");
}
}
?>
<title>onlinewebapplication.com Facebook | Twitter Login</title>
<style type="text/css">
#buttons
{
text-align:center
}
#buttons img,
#buttons a img
{ border: none;}
h1
{
font-family:Arial, Helvetica, sans-serif;
color:#999999;
}
</style>
<div id="buttons">
<h1>Twitter Facebook Login </h1>
<a href="?login&oauth_provider=twitter"><img src="images/tw_login.png"></a>
</div>
2. 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 '<h1>Welcome</h1>';
echo 'id : ' . $_SESSION['id'];
echo '<br/>Name : ' . $_SESSION['username'];
echo '<br/>Email : ' . $_SESSION['email'];
echo '<br/>You are login with : ' . $_SESSION['oauth_provider'];
echo '<br/>Logout from <a href="logout.php?logout">' . $_SESSION['oauth_provider'] . '</a>';
?>
3. twconfig.php
<?php
define('YOUR_CONSUMER_KEY', 'Your Twitter App key');
define('YOUR_CONSUMER_SECRET', 'Your Twitter App Security key');
?>
4. login-twitter.php
<?php
require("twitteroauth.php");
require '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://mydomain.com/getTwitterData.php');
// Saving them into the session
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
// If everything goes well..
if ($twitteroauth->http_code == 200) {
// Let's generate the URL and redirect
$url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
header('Location: ' . $url);
// header('Location: http://mydomain.com/home.php');
} else {
// It's a bad idea to kill the script, but we've got to know when there's an error.
die('Something wrong happened.');
}
?>
5. getTwitterData.php
<?php
ob_start();
require("twitteroauth.php");
require 'twconfig.php';
require 'functions.php';
session_start();
if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])) {
// We've got everything we need
$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
// Let's request the access token
$access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
// Save it in a session var
$_SESSION['access_token'] = $access_token;
// Let's get the user's info
$user_info = $twitteroauth->get('account/verify_credentials');
// Print user's info
echo '<pre>';
print_r($user_info);
echo '</pre><br/>';
if (isset($user_info->error)) {
// Something's wrong, go back to square 1
header('Location: login-twitter.php');
} else {
$twitter_otoken=$_SESSION['oauth_token'];
$twitter_otoken_secret=$_SESSION['oauth_token_secret'];
$email=$user_info->email;
$uid = $user_info->id;
$username = $user_info->name;
$loginname = $user_info->screen_name;
$aboutus = $user_info->description;
$location = $user_info->location;
$image = $user_info->profile_image_url_https;
$user = new User();
$userdata = $user->checkUser($uid, 'twitter', $username, $email, $loginname, $aboutus, $location, $image, $twitter_otoken, $twitter_otoken_secret);
if(!empty($userdata)){
session_start();
$_SESSION['id'] = $userdata['id'];
$_SESSION['oauth_id'] = $uid;
$_SESSION['username'] = $userdata['username'];
$_SESSION['oauth_provider'] = $userdata['oauth_provider'];
$_SESSION['uid']=$userdata ['user_id'];
$_SESSION['uid_token']=uniqid(bin2hex(mcrypt_create_iv(10, MCRYPT_DEV_RANDOM)));
mysql_query("UPDATE pr_user SET `user_session_token`='{$_SESSION['uid_token']}' WHERE `user_id`='{$res['user_id']}'");
header("Location: http://mydomain.com/home.php");
}
}
} else {
// Something's missing, go back to square 1
header('Location: login-twitter.php');
}
?>
6. functions.php
<?php
require 'dbconfig.php';
class User {
function checkUser($uid, $oauth_provider, $username, $email, $loginname, $aboutus, $location, $image, $twitter_otoken, $twitter_otoken_secret)
{
$query = mysql_query("SELECT * FROM `pr_user` WHERE user_logintype = '$oauth_provider' AND user_unique_id = '$uid'") or die(mysql_error());
$result = mysql_fetch_array($query);
if (!empty($result)) {
# User is already present
} else {
#user not present. Insert a new Record
list($city,$state) = explode(' ',$location);
$ip = $_SERVER['REMOTE_ADDR'];
$query = mysql_query("INSERT INTO `pr_user` (`user_type`, `user_logintype`, `user_unique_id`, `user_email`, `user_login`, `user_name`, `user_city`, `user_state`, `user_image`, `user_info`, `user_ip`)
VALUES ('user', '$oauth_provider', $uid, '$email', '$loginname', '$username', '$city', '$state', '$image', '$aboutus', '$ip')") or die(mysql_error());
$query = mysql_query("SELECT * FROM `pr_user` WHERE user_logintype = '$oauth_provider' AND user_unique_id = '$uid'");
$result = mysql_fetch_array($query);
return $result;
}
return $result;
}
}
?>
<?php
require 'dbconfig.php';
class User {
function checkUser($uid, $oauth_provider, $username, $email, $loginname, $aboutus, $location, $image, $twitter_otoken, $twitter_otoken_secret)
{
$query = mysql_query("SELECT * FROM `pr_user` WHERE user_logintype = '$oauth_provider' AND user_unique_id = '$uid'") or die(mysql_error());
$result = mysql_fetch_array($query);
if (!empty($result)) {
# User is already present
} else {
#user not present. Insert a new Record
list($city,$state) = explode(' ',$location);
$ip = $_SERVER['REMOTE_ADDR'];
$query = mysql_query("INSERT INTO `pr_user` (`user_type`, `user_logintype`, `user_unique_id`, `user_email`, `user_login`, `user_name`, `user_city`, `user_state`, `user_image`, `user_info`, `user_ip`)
VALUES ('user', '$oauth_provider', $uid, '$email', '$loginname', '$username', '$city', '$state', '$image', '$aboutus', '$ip')") or die(mysql_error());
$query = mysql_query("SELECT * FROM `pr_user` WHERE user_logintype = '$oauth_provider' AND user_unique_id = '$uid'");
$result = mysql_fetch_array($query);
return $result;
}
return $result;
}
}
?>
0 comments:
Post a Comment