All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Login With Facebook And Google PHP Code

Last Updated : Mar 11, 2024

Login With Facebook And Google PHP Code

In this article we will show you the solution of login with Facebook and google PHP code, the standard form-based login procedure that has long been a feature of PHP applications has a great competitor in the form-based Facebook login process.

You only need to add a Facebook button to the registration page to deploy it.

Once the user has successfully logged in, save their info in the following step.

In this tutorial, I'll go over how to integrate Facebook Login into PHP apps so you can collect user information.

We will now discuss the idea of how to login with facebook and google using php code with example.

Step By Step Guide On Login With Facebook And Google PHP Code :-

Config.php

<?php
 $config =array(
  "base_url" => "YOUR_WEBSITE_OAUTH_URL",
  "providers" => array (
   "Google" => array (
    "enabled" => true,
    "keys" => array ( "id" => "ABCD", "secret" => "XXXXXXXX" ),
   ),
   "Facebook" => array (
    "enabled" => true,
    "keys" => array ( "id" => "ABCD", "secret" => "XXXXXXXX" ),
   ),
   "Twitter" => array (
    "enabled" => true,
    "keys" => array ( "key" => "ABCD", "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" => "",
 );
?>

Index.php

<html>
<head>
 <title>Login With Facebook Twitter and Google using PHP - TalkersCode</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>
<?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>";
   }
        }
?>

Logout.php

<?php
session_start();
session_destroy();
header("Location: index.php");
?>
  1. The code below demonstrates how to implement OAuth authentication on a PHP-based website.
  2. As soon as the first line is executed, an array named $config is created to hold all configuration information.
  3. "base_url" specifies where the user will be redirected after successfully completing authentication in the $config array.
  4. As part of the "providers" array, a variety of OAuth providers such as Google, Facebook, and Twitter are listed, along with the configurations of each of their services.
  5. An authentication process is enabled by setting the "enabled" key-value pair to true for every OAuth provider.
  6. The client ID & client secret you acquired from your OAuth provider's developer dashboard can be found in the "keys" key.
  7. Debug logging is disabled by setting the "debug_mode" key to false, and enabled by setting it to true.
  8. When logging is enabled, "debug_file" specifies the file path to which the logs will be written.
  9. Following that, we have the following HTML file, which displays the logos of Facebook, Twitter, and Google for logging in. There is a title displayed in the browser tab, "Login With Facebook Twitter and Google using PHP - TalkersCode".
  10. Inside the body, a div element has the align attribute set to "center" so that the inside content will be aligned in the center.
  11. There is a heading and a subheading within the h1 element: "Facebook, Twitter, and Google integration using PHP".
  12. The next set of elements consists of three links (a elements) surrounded by images of the Facebook, Twitter, and Google logos. There are several links that point to the same login.php file, but each one has different parameters associated with it. Parameters indicating the social media provider that a user would like to log in with are listed here.
  13. For example, the Facebook login link includes "?provider=Facebook" at the end of the login.php file path. Additionally, the Twitter link has the "?provider=Twitter" and the Google link has the "?provider=Google" parameters.
  14. If a user clicks on any of these links, they will be redirected to the login.php file with the appropriate provider parameter passed along. A link will be provided in this file that will handle the login process with the selected social media platform and direct the user to the appropriate page once they have logged in.

Conclusion :-

As a result, we have successfully learned how to login with facebook and google using php code.

This lesson included a demonstration of how to incorporate a Facebook Login icon into a PHP application.

I hope this article on login with Facebook and google PHP code helps you and the steps and mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪