Cara login dengan gmail facebook php

Introducing the new FGlogin commercial edition, an OAuth login system for your website with Facebook and Google. FGLogin is very quick and powerful, sure this helps you to increase your web project registrations. It's definitely a must have login system for every PHP based web projects. Hardly it will take 5 mins for installation.

Cara login dengan gmail facebook php

Cara login dengan gmail facebook php
Download Script    
Cara login dengan gmail facebook php
Live Demo

Database
Users table database design.

CREATE TABLE IF NOT EXISTS `users`
(
id INT(11) NOT NULL AUTO_INCREMENT,
email VARCHAR(200) ,
name VARCHAR(200) ,
first_name VARCHAR(200) ,
last_name VARCHAR(200) ,
gender VARCHAR(10) ,
birthday VARCHAR(20) ,
location VARCHAR(200) ,
hometown VARCHAR(200) ,
bio TEXT,
relationship VARCHAR(30) ,
timezone VARCHAR(10) ,
provider VARCHAR(10) ,
provider_id INT(30) ,
picture TEXT,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;


The script contains three folders called facebook_lib,google_lib and images with PHP files.

facebook_lib //Facebook OAUTH library
-- config.php //Facebook app configuration file.
google_lib //Google OAUTH library
-- config.php //Google app configuration file.
images
db.php //Database connection
FGlogin.php //Class
facebook_login.php //Facebook Login
google_login.php //Google Login
index.php
home.php
logout.php


How it Works

Facebook & Google Authentication Flowchart

Cara login dengan gmail facebook php
Cara login dengan gmail facebook php

Installation
In this script just you have to modify three file.

db.php
Database configuration file, modify MySQL server details.

<?php
$mysql_hostname = "localhost";
$mysql_user = "username";
$mysql_password = "password";
$mysql_database = "databasename";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>


Application setup details click here.

Facebook Configuration facebook_lib/config.php
You have to create a application. Facebook will provide you app id and app secret id, just replace in the following code.
fbconfig.php

<?php
$facebook_appid='App ID';
$facebook_app_secret='App Secret';
$facebook = new Facebook(array(
'appId' => $facebook_appid,
'secret' => $facebook_app_secret,
));
?>

Google Configuration google_lib/config.php
You can find this in google_lib folder, here you have to configure application OAuth keys, Consumer keys and redirection callback URL.

// OAuth2 Settings, you can get these keys at https://code.google.com/apis/console Step 6 keys 
'oauth2_client_id' => 'App Client ID',
'oauth2_client_secret' => 'App Client Secret',
'oauth2_redirect_uri' => 'http://yoursite.com/gplus/index.php',

// OAuth2 Settings Step 3  keys.
'oauth_consumer_key' => 'OAuth Consumer Key',
'oauth_consumer_secret' => 'OAuth Consumer Secret',



In this tutorial, I have explained how to add Login with Facebook, Twitter and Google functionality to your website using PHP. I have used HybridAuth PHP library to achieve this.

List of identity providers supported by HybridAuth Library.

  • Facebook
  • Twitter
  • Google
  • Yahoo
  • LinkedIn
  • Live
  • AOL
  • MySpace
  • Github

Cara login dengan gmail facebook php
Cara login dengan gmail facebook php

Follow the below steps to create Login with Facebook,Twitter and Google widget. Below image explains how hybridAuth library works.

Cara login dengan gmail facebook php

Step 1) Download HybridAuth PHP library from Github .

Step 2). You need to get Developer API(OAuth) Key and Secret from Facebook, Twitter and Google.

Step 3). Create config.php and add below code to the file.

$config = array("base_url" => "YOUR_WEBSITE_OAUTH_URL", 
		"providers" => array ( 
			"Google" => array ( 
				"enabled" => true,
				"keys"    => array ( "id" => "YOUR_GOOGLE_API_KEY", "secret" => "YOUR_GOOGLE_API_SECRET" ), 

			),

			"Facebook" => array ( 
				"enabled" => true,
				"keys"    => array ( "id" => "FACEBOOK_DEVELOER_KEY", "secret" => "FACEBOOK_SECRET" ),
				"scope" => "email, user_about_me, user_birthday, user_hometown"	//optional. 			 
			),

			"Twitter" => array ( 
				"enabled" => true,
				"keys"    => array ( "key" => "TWITTER_DEVELOPER_KEY", "secret" => "TWITTER_SECRET" ) 
			),
		),
		// 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" => "debug.log",
	);

Note: Set API key and secrets for Google, Facebook and Twitter. base_url is the OAuth Callback page. In demo page, base_url=”http://hayageek.com/examples/oauth/hybridauth/hybridauth/index.php”. Content of the base_url is.

<?php
require_once( "Hybrid/Auth.php" );
require_once( "Hybrid/Endpoint.php" );
Hybrid_Endpoint::process();
?>

Note: In Google API OAuth Settings set your redirect URL like this http://hayageek.com/examples/oauth/hybridauth/hybridauth/index.php?hauth.done=Google

Step 4). Create login.html and add below code

<html>
<body>
	<a href="login-with.php?provider=Facebook">Login With Facebook</a>
	<a href="login-with.php?provider=Twitter">Login With Twitter</a>
	<a href="login-with.php?provider=Google">Login With Google</a>

</body>
</html>

Step 5) Create login-with.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>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.";
                         $authProvider->logout();
                         break;
                case 7 : echo "User not connected to the provider.";
                         $authProvider->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>";

	}

}
?>

Step 6) Create logout.php with the below code.

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