Skip to content

Web and Mobile App Development – sisnolabs

Recent Posts

  • Props and state in ReactJS
  • A Simple ReactJS Form Example
  • ReactJS Router – Navigation In React
  • ReactJS Introduction, File Structure, CSS Implementation
  • List of important custom and in-built AMP tags
  • AMP (Accelerated Mobile Pages) Example Pages
  • Upgrade Laravel 4.2 to 5.0 and above version
  • Introduction to laravel 5.x application structure
  • Facebook Twitter login PHP
  • Gzip compression – to make website load faster

Navigation

  • Software Services
  • Development Portfolio
  • Client Testimonials
  • About sisnolabs
  • Request a free quote
  • Careers

Tags

  • AMP
  • AMP tutorial
  • canvas example
  • Canvas Examples
  • css3
  • css tips
  • CSS tutorial
  • Facebook Login
  • gzip compression
  • html
  • html5
  • html5 canvas
  • html5 tutorial
  • html form
  • improve website speed
  • Laravel
  • Laravel Tutorial
  • modern web development
  • php7
  • PHP SDK
  • ReactJS
  • react tutorial
  • tutorial
  • Twitter Oauth Login
  • web design
  • web development

Tag: Twitter Oauth Login

Facebook Twitter login PHP

Other than manual user registration, do you wish to give your website users to auto login through facebook twitter? It is very useful because users do not need to remember login details, they can directly login/signup using Facebook Twitter.

 

Login with Facebook PHP SDK

You can download Facebook php SDK scripts here – https://github.com/facebook/facebook-php-sdk

 

Facebook Application Setup

Go to the https://developers.facebook.com/apps and create Facebook App. You will get app id and app secret id, just modify in the following code.

<? php
require 'facebook/facebook.php'; $facebook = new Facebook(array( 'appId' => "YOUR APP ID",
‘secret’ => “YOUR APP SECRET”,));$user = $facebook->getUser();

if ($user) {

try {

// Proceed knowing you have a logged in user who’s authenticated.
$userInfo = $facebook->api(‘/me’);

} catch (FacebookApiException $e) {

error_log($e);

$user = null;

}

if (!empty($userInfo)) {
//User info ok? Let’s print it
//Here we will be adding the login and registering routines.

} else {

//if error, distroy session and redirect to facebook login page
session_destroy();
session_unset();
$login_url = $facebook->getLoginUrl(array( ‘scope’ => ’email’));
header(“Location: ” . $login_url);
exit;
}

} else {

# There’s no active session, let’s generate one
$login_url = $facebook->getLoginUrl(array( ‘scope’ => ’email’));

if(trim($_GET[‘error’]) == ‘access_denied’){

//if user denied, redirect to home page

}else{

header(“Location: ” . $login_url);
exit;

}

}

? >

 

This script will return all user details in array, now you can save these details in your database or file or session and make user login in your website.

 

Login with Twitter

 

First of all you have to create twitter app with your account. Login into twitter developer account and create app for your website and get your “Consumer Key” and “Consumer Secret”.

Create Twitter App here – https://apps.twitter.com/app/new

 

We need twitter Oauth class, you can download it here – https://github.com/abraham/twitteroauth.

 

Now just follow the below steps.

1) Create login-twitter.php file and copy the following code in it.

<? php
require("twitteroauth/twitteroauth.php");
session_start();

$twitteroauth = new TwitterOAuth('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET');
// Requesting authentication tokens, the parameter is the URL we will be redirected to
$request_token = $twitteroauth->getRequestToken(‘http://yourdomain.com/twitter-callback.php’);

// Saving them into the session

$_SESSION[‘oauth_token’] = $request_token[‘oauth_token’];
$_SESSION[‘oauth_token_secret’] = $request_token[‘oauth_token_secret’];

// If everything goes well..
if ($twitteroauth->http_code == 200) {

// Let’s generate the URL and redirect
$url = $twitteroauth->getAuthorizeURL($request_token[‘oauth_token’]);
header(‘Location: ‘ . $url);
exit;

} else {

// It’s a bad idea to kill the script, but we’ve got to know when there’s an error.
die(‘Something wrong happened.’);

} ? >

Don’t forget to replace your ‘YOUR_CONSUMER_KEY’, ‘YOUR_CONSUMER_SECRET’ and your callback url path.

 

2) Now create  twitter-callback.php file and copy the following code in it.

<? php
require("twitteroauth/twitteroauth.php");
session_start();

if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])) {
// We’ve got everything we need
$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, $_SESSION[‘oauth_token’], $_SESSION[‘oauth_token_secret’]);

// Let’s request the access token
$access_token = $twitteroauth->getAccessToken($_GET[‘oauth_verifier’]);

// Save it in a session var
$_SESSION[‘access_token’] = $access_token;

// Let’s get the user’s info
$user_info = $twitteroauth->get(‘account/verify_credentials’);

if (isset($user_info->error)) {

// Something’s wrong, go back.

} else {

//here you get user detail object,
//now you can store user detail in database or in session and make user login.

$_SESSION[‘id’] = $user_info->id;
$_SESSION[‘username’] = $user_info->name;
$_SESSION[‘image’] = $user_info->profile_image_url;
$_SESSION[‘screenname’] = $user_info->screen_name;

//you can store user detail as per your requirment.
}

} else {

if($_GET[‘denied’] != “”){

//if user denied, go back to your home page
header(‘Location: http://www.yourdomain.com/index.php’);
exit;

}else{

// Something’s missing, go back to square 1
header(‘Location: http://www.yourdomain.com/login-twitter.php’);
exit;
}
} ? >

 

Thats it!

 

Posted on September 26, 2014September 28, 2020Author mobandTags Facebook Login, PHP SDK, Twitter Oauth Login, web development
Proudly powered by WordPress