Login with Facebook and Twitter using PHP

Facebook and Twitter is the most popular social network and both offering oAuth support. So here in this tutorial, I have explained, how to implement Login with Facebook and Twitter functionality to your website using PHP.

Here in this tutorial, I have explained simple steps to implement Login with Facebook and Twitter with live demo.

Also, read:

So let’s start the coding

Steps1: As I have used HybridAuth Library with this example, so you need download HybridAuth PHP library from Github.


Steps2: You also need to get Developer API(OAuth) Key and Secret from Facebook and Twitter.

Steps3: Now you need to create config.php and add below code to the file.

$config =array(
"base_url" => "http://phpzag.com/demo/login-with-facebook-twitter-google-php/hybridauth/index.php",
"providers" => array (
"Facebook" => array (
"enabled" => true,
"keys" => array ( "id" => "XXXXXXXXXXXX", "secret" => "XXXXXXXXXXXX" ),
),
"Twitter" => array (
"enabled" => true,
"keys" => array ( "key" => "XXXXXXXXXXXX", "secret" => "XXXXXXXXXXXX" )
),
),
"debug_mode" => false,
"debug_file" => "",
);

Steps4: Now create index.php and add below code.

<div align="center">
<a href="login.php?provider=Facebook"><img 
src='images/facebook.png'></img></a> <br><br>
<a href="login.php?provider=Twitter"><img 
src='images/twitter.png'></img></a> <br><br>
</div>

Steps5: You will need to create login.php and add below code.

<?php
session_start();
include('config.php');
include('hybridauth/Hybrid/Auth.php');
if(isset($_GET['provider'])) {
$provider = $_GET['provider'];
try {
$hybridauth = new Hybrid_Auth( $config );
$authProvider = $hybridauth->authenticate($provider);
$user_profile = $authProvider->getUserProfile();
if($user_profile && isset($user_profile->identifier)) {
echo "<b>User Name</b> :".$user_profile->displayName."<br>";
echo "<b>Profile URL</b> :".$user_profile->profileURL."<br>";
echo "<b>Profile Image</b> :".$user_profile->photoURL."<br> ";
echo "<img src='".$user_profile->photoURL."'/><br>";
echo "<b>Email</b> :".$user_profile->email."<br>";
echo "<br> <a href='logout.php'>Logout</a>";
}
}
catch( Exception $e ) {
switch( $e->getCode() ) {
case 0 : echo "Unspecified error."; break;
case 1 : echo "Hybridauth configuration error."; break;
case 2 : echo "Provider not properly configured."; break;
case 3 : echo "Unknown or disabled provider."; break;
case 4 : echo "Missing provider application credentials."; break;
case 5 : echo "Authentication failed. "
. "The user has canceled the authentication or the provider refused the connection.";
break;
case 6 : echo "User profile request failed. Most likely the user is not connected "
. "to the provider and he should to authenticate again.";
$twitter->logout();
break;
case 7 : echo "User not connected to the provider.";
$twitter->logout();
break;
case 8 : echo "Provider does not support this feature."; break;
}
echo "<br /><br /><b>Original error message:</b> " . $e->getMessage();
echo "<hr /><h3>Trace</h3> <pre>" . $e->getTraceAsString() . "</pre>";
}
}
?>

Steps6: Finally create logout.php with the below code to handle logout functionality.


<?php
session_start();
session_destroy();
header("Location: index.php");
?>

You may also like:

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo Download