Twitter login using PHP and Oauth

Nowadays most of website use social media login, Because owners want every visitors info and also visitors haven’t time to fill long registration form. As good blogger or developer you must have knowledge of how to add social media login. In this article, I will create a tutorial on how to add twitter login using PHP. You show have basic knowledge of php.

Before starting, I want to share with you that a twitter doesn’t share email of logged in user. So if your website or blog need user’s email twitter can’t give you. Firstly before begin ,you should have consumer key and screat key if you have not then create twitter app and generate key first.

I already write tutorial on previous article, how to post on twitter using php, read it and create app first. After create app and collect key, i will create 4 files. Fist look the final layout what we want to make.

Twitter Login using PHP

First Create twitterconfi.php it store all keys and token.

<?php
define('CONS_KEY', '');
define('CONS_SECRET','');
?>

Now create index.php file look like below. If user already registered , it show user’s info else login button. I added some extra stylesheet for design, you don’t have need to add css.

<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="container">
<center>
<div class="header">
    <div class="logo">&nbsp;</div>
    </div>
    <hr style="width:80%" />
	<?php  if(isset($_SESSION['access_token'])!='') 
	{
	   $uid=$_SESSION['twid'];
	   $username=$_SESSION['tname'];
	   $user_s_name=$_SESSION['tsname'];
	echo "<h1>Welcome ".$username." | <a href=logout.php title=Twitter Login>Logout</a> <br />(Userid - ".$uid.") (Username ".$user_s_name.")</h1>";
	} 
	else
	{
		echo "<h2>Welcome <a href=twi_login.php title=Twitter Login>Login</a></h2>";		
	}
	?> 
    <div class="article1">Back to Tutorial</div>
    <div class="article2">Join us Facebook</div>
    <div class="article3">Follow us Twitter</div>
    <div class="article4">Download</div><br />
</center>
</div>
</body>
</html>

You can put login page’s link anywhere in your website. If session is empty then user will get login link. this code is for login file and get authorized token from twitter. Note, You can download additional files with tutorial download link.

<?php
session_start();
require("twitter/twitteroauth.php");
require 'twitter/twconfig.php';
$twitteroauth = new TwitterOAuth(CONS_KEY, CONS_SECRET);
$request_token = $twitteroauth->getRequestToken('yoururl/twit/twitterdata.php');
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
if ($twitteroauth->http_code == 200) 
{
    $url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
    header('Location: ' . $url);
} else {
    die('ERROR Huh... Check Your Code and My Article Again');
}
?>

If twitter authorized success above code create session and then send user to twitterdata.php file, look like below. It stores user data as session and then get back user to index.php.

<?php
session_start();
require("twitter/twitteroauth.php");
require 'twitter/twconfig.php';
if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])) 
{
    $twitteroauth = new TwitterOAuth(CONS_KEY, CONS_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
    $access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
    $_SESSION['access_token'] = $access_token;
    $user_info = $twitteroauth->get('account/verify_credentials');
    if (isset($user_info->error)) 
    {
        header('Location: login-twitter.php');
    } 
	else 
	{
	   $twitter_otoken=$_SESSION['oauth_token'];
	   $twitter_otoken_secret=$_SESSION['oauth_token_secret'];
	   $email='';
       $uid = $user_info->id;
       $username = $user_info->name;
	   $userimg = $user_info->screen_name;	   
	   $_SESSION['twid']=$uid ;
	   $_SESSION['tname']=$username;
	   $_SESSION['tsname']=$userimg;
   	   header('Location: index.php');  
    }
}
else 
{
    header('Location: login.php');
}
?>

It’s completely over this file store all token in session veriable and get back user to index.php. At last this is logout.php code.

<?php
session_start();
session_unset();
session_destroy();
header('Location: index.php');
?>

I hope this tutorial will helps you, if you found and bug or error let me know. I will fix it soon.

Savan Paun

I am Savan Paun Admin of this blog. I like to write about Wordpress, Design, Development, Plugin, SEO, Blogging and Making Money online. I am a professional blogger based in Jamnagar. I'm always available for beginner who wants to learn.

Leave a Reply