Twitter Connect – Twitter Login System with PHP and MySQL
In this tutorial you will see how to make a Login System with Twitter using php and mysql. User will login with their Twitter account, give permission to access basic profile, and their name with email will be stored in database if they are not stored before.
Live Demo
How to Create a Twitter Application for Website Login
In case if you are a newbie, Watch the following Video before proceeding.
Steps to Create Twitter Login System with PHP and MySQL
Lets Code Now, Create a Database named “login_system” or Anything and Create a Table “users_twitter”
1 2 3 4 5 6 7 8 |
CREATE DATABASE IF NOT EXISTS login_system; USE login_system; CREATE TABLE IF NOT EXISTS `users_twitter` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `twitter_id` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; |
I hope you have created a Twitter app to get CLIENT_ID and APP_SECRET. Set the config.php with all your site and Twitter related credentials
1 2 3 4 5 6 7 8 |
define("CLIENT_ID", "your client ID"); define("SECRET_KEY", "your secret key"); /* make sure the url end with a trailing slash, give your site URL */ define("SITE_URL", "http://localhost/twitter-connect-login-system-php-mysql/"); /* the page where you will be redirected for authorization */ define("REDIRECT_URL", SITE_URL."twitter_login.php"); define("LOGOUT_URL", SITE_URL."logout.php"); |
Set your index.php with the login URL in the anchor tag. So when the user clicks on it it will be redirected to Twitter site.
1 2 3 4 5 |
<!-- Add login URL in index.php --> <a class="btn btn-block btn-social btn-twitter" href="<?php echo $loginURL; ?>"><i class="fa fa-twitter"></i> Login with Twitter</a> <!-- Add logout URL in home.php --> <a class="btn btn-block btn-social btn-twitter" href="<?php echo $logoutURL; ?>">Logout</a> |
User will login with their Twitter accounts and give permission to the application. If the user gives permission to the app then user information will be returned and data will be processed accordingly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
require('http.php'); require('oauth_client.php'); require('config.php'); $client = new oauth_client_class; $client->redirect_uri = REDIRECT_URL; $client->client_id = CLIENT_ID; $application_line = __LINE__; $client->client_secret = SECRET_KEY; if (strlen($client->client_id) == 0 || strlen($client->client_secret) == 0) die('set your access'); if (($success = $client->Initialize())) { if (($success = $client->Process())) { if (strlen($client->access_token)) { $success = $client->CallAPI( 'https://api.twitter.com/1.1/account/verify_credentials.json', 'GET', array(), array('FailOnAccessError' => true), $user); } } $success = $client->Finalize($success); } if ($client->exit) exit; |
And finally Update or Insert User data in Database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
if ($success) { // Now check if user exist with same email ID $sql = "SELECT COUNT(*) AS count from users_twitter where twitter_id = :id"; try { $stmt = $DB->prepare($sql); $stmt->bindValue(":id", $user->id); $stmt->execute(); $result = $stmt->fetchAll(); if ($result[0]["count"] > 0) { // User Exist $_SESSION["name"] = $user->name; $_SESSION["id"] = $user->id; $_SESSION["new_user"] = "no"; } else { // New user, Insert in database $sql = "INSERT INTO `users_twitter` (`name`, `twitter_id`) VALUES " . "( :name, :id)"; $stmt = $DB->prepare($sql); $stmt->bindValue(":name", $user->name); $stmt->bindValue(":id", $user->id); $stmt->execute(); $result = $stmt->rowCount(); if ($result > 0) { $_SESSION["name"] = $user->name; $_SESSION["id"] = $user->id; $_SESSION["new_user"] = "yes"; $_SESSION["e_msg"] = ""; } } } catch (Exception $ex) { $_SESSION["e_msg"] = $ex->getMessage(); } $_SESSION["user_id"] = $user->id; } else { $_SESSION["e_msg"] = $client->error; } |
and That’s it. Your Twitter Login System with PHP & MySQL is Ready. 🙂
Live Demo