Sign in with Twitter using PHP

With millions of registered users worldwide, twitter is one of the most used social networking website on the internet, we cannot overlook its importance, it can really boost registrations rate in your site. In this tutorial, we will be using Twitter API to register users on your website. I have created 3 PHP files for the tutorial : configuration, Login and process PHP files. We also need Abraham William's Twitter PHP Library, which is a widely used and known for its simplicity. I have included this library in downloadable file along with tutorial files below.


Configuration file stores your Twitter Customer key, secret and callback URL. If you haven't created Twitter application for your website, go to Twitter developer page and create one here. Once you finish creating Twitter App, you need to get your Customer key & Secret, and replace config variables in config.php file.[cc lang="php"] [/cc]

Login page

Login Page (index.php) contains a login button, but you can put login button anywhere in your website. Once user clicks login, user must be redirected to process.php, from where user is sent to Twitter Auth page to obtains a request token, and again user is redirected back to process.php. On successful authorization, process.php sets details in session variables which will be used later in other pages to make GET/POST requests.Trick is very simple, if this session is not set login button must be displayed in order to redirect use to Twitter authorization page.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
if(isset($_SESSION['status']) && $_SESSION['status']=='verified') { // user is logged in }else{ //show login button }
Complete code of login page. [cc lang="html"] Sign-in with Twitter
Welcome '.$screenname.' (Twitter ID : '.$twitterid.'). Logout!
'; $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);//see if user wants to tweet using form. if(isset($_POST["updateme"])) { //Post text to twitter $my_update = $connection->post('statuses/update', array('status' => $_POST["updateme"])); die(''); //redirect back to index.php }//show tweet form echo '
'; echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
'; echo '
';//Get latest tweets $my_tweets = $connection->get('statuses/user_timeline', array('screen_name' => $screenname, 'count' => 5)); /* echo '
'; print_r($my_tweets); echo '
'; */echo '
Latest Tweets : '; echo '
    '; foreach ($my_tweets as $my_tweet) { echo '
  • '.$my_tweet->text.'
  • '; } echo '
';}else{ //login button echo ''; }?>


Main task of process.php  is to compare variables and redirect user back and forth. When user clicks on login button in index.php, user is sent to process.php, then it obtains a request token which is passed to Twitter Authorize page as oauth_token parameter. Once user signs in, user is authenticated and returned to the callback URL.[cc lang="php"] getAccessToken($_REQUEST['oauth_verifier']); if($connection->http_code=='200') { //redirect user to twitter $_SESSION['status'] = 'verified'; $_SESSION['request_vars'] = $access_token;// unset no longer needed request tokens unset($_SESSION['token']); unset($_SESSION['token_secret']); header('Location: ./index.php'); }else{ die("error, try again later!"); }}else{if(isset($_GET["denied"])) { header('Location: ./index.php'); die(); }//fresh authentication $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); $request_token = $connection->getRequestToken(OAUTH_CALLBACK);//received token info from twitter $_SESSION['token'] = $request_token['oauth_token']; $_SESSION['token_secret'] = $request_token['oauth_token_secret'];// any value other than 200 is failure, so continue only if http code is 200 if($connection->http_code=='200') { //redirect user to twitter $twitter_url = $connection->getAuthorizeURL($request_token['oauth_token']); header('Location: ' . $twitter_url); }else{ die("error connecting to twitter! try again later!"); } } ?> [/cc] I hope this tutorial will help you, Once you understand the flow, it will be easier to implement it on your website. any feedback or comment is appreciated, Good luck!Download Demo
New question is currently disabled!