• Login with Twitter using PHP

    “Sign in with Twitter” is the pattern of authentication that allows users to connect their Twitter account with third-party services in as little as one click. It utilizes OAuth and although the flow is very similar, the authorization URL and workflow differs slightly as described below.

    Your are easy way to implement twitter login in your web site using following step:

    Download : https://github.com/abraham/twitteroauth

    Database :

    Sample database users table columns id, email, oauth_uid, oauth_provider and username.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    CREATE TABLE users
     
    (
     
    id INT PRIMARY KEY AUTO_INCREMENT,
     
    email VARCHAR(70),
     
    oauth_uid VARCHAR(200),
     
    oauth_provider VARCHAR(200),
     
    username VARCHAR(100),
     
    twitter_oauth_token VARCHAR(200),
     
    twitter_oauth_token_secret VARCHAR(200)
     
    );

    Twitter Setup

    Create a twitter application click here. Some Twitter provide you consumer key and consumer secret key using these modify following code.

    twconfig.php

    1
    
     

    dbconfig.php

    Database configuration file.

    1
    
     

    login-twitter.php

    In root directory find out the below line at login-twitter.php code and replaceyourwebsite.

    $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
    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
    39
    40
    
    <!--?php 
     
    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");
     
    }
     
    else if ($oauth_provider == 'facebook')
     
     {
     
    header("Location: login-facebook.php");
     
    }
     
    }
     
    ?-->
     
    //HTML Code
    <a href="?login&amp;oauth_provider=twitter">Twitter_Login</a>
    Tags:

Comments on this post

Leave a Reply

  • Security Code :


    five × = 15