Login With Facebook Twitter and Google using PHP – onlinecode

Login With Facebook Twitter and Google using PHP – onlinecode

In this post we will give you information about Login With Facebook Twitter and Google using PHP – onlinecode. Hear we will give you detail about Login With Facebook Twitter and Google using PHP – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this tutorial, We have explained how to Login With Facebook Twitter and Google using PHP. I have used HybridAuth PHP library to achieve this. we have used the HybridAuth PHP library to make social login example.

Step 1: Download the HybridAuth PHP library.Here in this step download the HybridAuth PHP library using following below Url.https://github.com/hybridauth/hybridauth/archive/master.zip

Step 2: We also need to get the Developer API(OAuth) Key and Secret from Facebook, Twitter, and Google.

Step 3: Now create a config.php file and set the base_url, keys_id, and keys_secret. see the below following code.

<?php
	$config =array(
		"base_url" => "YOUR_WEBSITE_OAUTH_URL", 
		"providers" => array ( 

			"Google" => array ( 
				"enabled" => true,
				"keys"    => array ( "id" => "XXXXXXXXXXXX", "secret" => "XXXXXXXX" ), 
			),

			"Facebook" => array ( 
				"enabled" => true,
				"keys"    => array ( "id" => "XXXXXXXXX", "secret" => "XXXXXXXX" ), 
			),

			"Twitter" => array ( 
				"enabled" => true,
				"keys"    => array ( "key" => "XXXXXXXX", "secret" => "XXXXXXX" ) 
			),
		),
		// if you want to enable logging, set 'debug_mode' to true  then provide a writable file by the web server on "debug_file"
		"debug_mode" => false,
		"debug_file" => "",
	);
?>

Step 4: create an index.php file and add below code.

<html>
<head>
	<title>Login With Facebook Twitter and Google using PHP - onlinecode</title>
</head>
<body align="center">	
	<div align="center">
		<h1>Login With Facebook Twitter and Google using PHP</h1>
		<a href="http://tridentsportscars.com/?big=login.php?provider=Facebook"><img src="http://tridentsportscars.com/?big=images/facebook.png"></img></a> <br><br>
		<a href="http://tridentsportscars.com/?big=login.php?provider=Twitter"><img src="http://tridentsportscars.com/?big=images/twitter.png"></img></a> <br><br>
		<a href="http://tridentsportscars.com/?big=login.php?provider=Google"><img src="http://tridentsportscars.com/?big=images/google.png"></img></a><br><br>
	</div>
</body>
</html>

Step 5: Create a login.php file and add the 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>Name</b> :".$user_profile->displayName."<br>";
	        	echo "<b>Profile URL</b> :".$user_profile->profileURL."<br>";
	        	echo "<b>Image</b> :".$user_profile->photoURL."<br> ";
	        	echo "<img src="http://tridentsportscars.com/?big=".$user_profile->photoURL."'/><br>";
	        	echo "<b>Email</b> :".$user_profile->email."<br>";	        		        		        	
	        	echo "<br> <a href="http://tridentsportscars.com/?big=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;
                }

                // well, basically your should not display this to the end user, just give him a hint and move on..
                echo "<br /><br /><b>Original error message:</b> " . $e->getMessage();

                echo "<hr /><h3>Trace</h3> <pre>" . $e->getTraceAsString() . "</pre>";

			}
        
        }
?>

Step 6: Finally create a logout.php file and add the below code.

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

Download

Please follow and like us:

Hope this code and post will helped you for implement Login With Facebook Twitter and Google using PHP – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

15  +    =  20

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US