Login with Facebook and Twitter

March 17th, 2012 by laeeq | 4 comments

As we know Facebook and Twitter have become large in the social network world and both networks offering oAuth support. Nowadays web users not interested to filling the big registration forms. This tutorial 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.

  1. CREATE TABLE users
  2. (
  3. id INT PRIMARY KEY AUTO_INCREMENT,
  4. email VARCHAR(70),
  5. oauth_uid VARCHAR(200),
  6. oauth_provider VARCHAR(200),
  7. username VARCHAR(100),
  8. twitter_oauth_token VARCHAR(200),
  9. twitter_oauth_token_secret VARCHAR(200)
  10. );

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

  1. <?php
  2. define(‘APP_ID’, ‘Facebook APP ID’);
  3. define(‘APP_SECRET’, ‘Facebook Secret ID’);
  4. ?>

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

  1. <?php
  2. define(‘YOUR_CONSUMER_KEY’, ‘Twitter Key’);
  3. define(‘YOUR_CONSUMER_SECRET’, ‘Twitter Secret Key’);
  4. ?>

dbconfig.php
Database configuration file.

  1. <?php
  2. define(‘DB_SERVER’, ‘localhost’);
  3. define(‘DB_USERNAME’, ‘User Name’);
  4. define(‘DB_PASSWORD’, ‘Password’);
  5. define(‘DB_DATABASE’, ‘DATABASE’);
  6. $connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
  7. $database = mysql_select_db(DB_DATABASE) or die(mysql_error());
  8. ?>

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’);

index.php
If you want to modify your web project existing login or index pages, just use following code.

  1. <?php
  2. session_start();
  3. if (isset($_SESSION['id'])) {
  4. // Redirection to login page twitter or facebook
  5. header(“location: home.php”);
  6. }
  7. if (array_key_exists(“login”, $_GET))
  8. {
  9. $oauth_provider = $_GET['oauth_provider'];
  10. if ($oauth_provider == ‘twitter’)
  11. {
  12. header(“Location: login-twitter.php”);
  13. }
  14. else if ($oauth_provider == ‘facebook’)
  15. {
  16. header(“Location: login-facebook.php”);
  17. }
  18. }
  19. ?>

//HTML Code

  1. <a href=“?login&oauth_provider=twitter”>Twitter_Login</a>
  2. <a href=“?login&oauth_provider=facebook”>Facebook_Login</a>

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Wrapping Text using PHP
  • PHP 5.5 beta 4 is now available
  • Swapping Array Keys and Values
  • PHP Security
  • Working with Image Metadata in PHP
  • Converting Between Relative and Absolute File Path
  • Parsing File Paths with PHP
  • How to remove HTML Comments with PHP?
  • How to access .htaccess values?
  • Dealing With Common PHP Errors
  • Interview Questions and Answers for Freshers
  • Display numbers with ordinal suffix using PHP
  • Function to set COLLATION on database fields of Mysql
  • Read and write to remote files using PHP
  • Sharing PHP SESSION Variables between Multiple Subdomains
  • PHP 5.5.0 Alpha4 Development Preview Released
  • Check a string starts with http using PHP
  • Cross-Site Scripting Attacks (XSS)
  • PHP 5.4.11 and PHP 5.3.21 released!
  • PHP Memory Leak Issue
  •  

     

    1. March 23rd, 2012 at 11:15 | #1

      Fairly good post. I actually just came on your current blog plus wanted to talk about i need truly really liked studying your own blog posts. Even so I am going to often be following to the wesite plus I really hope you actually post again quickly.

    2. arianacookgv
      September 28th, 2012 at 15:35 | #2

      Wonderful web site. Lots of useful info here. I am sending it to several pals ans also sharing in delicious. And certainly, thank you in your sweat!

    3. christopherlongay
      October 3rd, 2012 at 16:47 | #3

      Great site. Plenty of helpful info here. I am sending it to some pals ans additionally sharing in delicious. And naturally, thank you to your effort!

    1. May 12th, 2012 at 06:26 | #1