In this tutorial, We have explained how to Login With Facebook Twitter and Google using PHP. php login with facebook google twitter or Login With Facebook Twitter and Google using PHP.
Login With Facebook, Twitter and Google using PHP – itsolutionstuck
In this post we will give you information about Login With Facebook Twitter and Google using PHP – itsolutionstuck. Hear we will give you detail about Login With Facebook Twitter and Google using PHP – itsolutionstuck And 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 - itsolutionstuck</title> </head> <body align="center"> <div align="center"> <h1>Login With Facebook Twitter and Google using PHP</h1> <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> <a href="login.php?provider=Google"><img src='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='".$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; } // 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