Skip to content

Web and Mobile App Development – sisnolabs

Recent Posts

  • How to Develop Your Freelancing Brand
  • How to start preparing for your freelancing career as early as High School
  • How to Succeed at Being a Freelancer
  • 10 Tax Deductions Every Freelancer Should Know About
  • How to Deal with a Difficult Client as a Freelancer
  • 10 Ways Freelancers can Make Money Online
  • Here are 9 Benefits of Hiring Freelancers for Your Business
  • 5 Things You Should do to Take Better Care of Yourself
  • What Everyone Needs to Know About DACA and Dreamers
  • Coffee Shops All Over Are Cutting Off Wi-Fi Because Of Freelancers

Navigation

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

Tags

  • AMP
  • AMP tutorial
  • business tips
  • canvas example
  • Canvas Examples
  • css3
  • css tips
  • CSS tutorial
  • Facebook Login
  • freelancer
  • Freelance Writer
  • gzip compression
  • Hiring Tips
  • html
  • html5
  • html5 canvas
  • html5 tutorial
  • html form
  • improve website speed
  • Laravel
  • Laravel Tutorial
  • modern web development
  • php7
  • PHP SDK
  • ReactJS
  • react tutorial
  • remote work
  • tutorial
  • Twitter Oauth Login
  • web design
  • web development
  • work from home

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!

 

Tags Facebook Login, PHP SDK, Twitter Oauth Login, web development