Login with Google


Google Login API allows the user to sign into the website using their Google account without sign up on that website. Here we can give an option to the user to register/ login to our system with their Google  Id. The process is like this: If the user click on "Login with Google" button then it will ask Google user name and password. If the user is using the system first time then it will fill up the registration form and display first name, last name, email and Gender fields. There will be no password field as the user is register / connect through Gmail. Once the user click on submit, our system will save the data in the server with the reference number sent by Google. Next time when the user come and click on "Login with Google" our system will check the reference number in our database, if it matches then it will sent the user directly to My Account page.

Please note in this process we will not save password in the database. You can follow the same steps for login with yahoo and other social media login/ registration system.

You can download the attached file and get the basic html form and other files used in this project.

You need to generate Google App Client Id and Client Secret.

Creating a Google App

Craete Login/ Registration system Step1:

Save the App Client ID and Client Secret and Redirect URL.

<?php
/* Google App Client Id */
define('CLIENT_ID', 'xxxxxxxxxxxxxxxxxxxx');
/* Google App Client Secret */
define('CLIENT_SECRET', 'xxxxxxxxxxxxxxxxxxxx');
/* Google App Redirect Url */
define('CLIENT_REDIRECT_URL', 'xxxxxxxxxxxxxxxxxxxx');
?>


Now Add the following code to "Login with Google" button.

<a href="<?php echo $login_url ?>">Login with Google</a>

If the user enters the correct info then Google page will redirect back to or login page with required information's.

if(isset($_GET['code'])) {
try {
$gapi = new GoogleLoginApi();
// Get the access token 
$data = $gapi->GetAccessToken(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $_GET['code']);
// Get user information
$user_info = $gapi->GetUserProfileInfo($data['access_token']);

$email  	= $user_info['emails'][0]['value'];
$id		= $user_info['id'];
$fullname	= $user_info['displayName'];
$fname  	= $user_info['name']['givenName'];
$lname  	= $user_info['name']['familyName'];
$gender		= $user_info['gender'];
$image		= $user_info['image']['url'];
$loginwith	= "Google";
}
catch(Exception $e) {
echo $e->getMessage();
exit();
}
}

If the user registered before then our system will redirect the user to my account page otherwise it will ask the user to register for the first time.

if ($stage==2 && $csrf == $_SESSION["token"]) {
$fname 		= $connect->real_escape_string($_POST['fname']);
$lname 		= $connect->real_escape_string($_POST['lname']);
$email 		= $connect->real_escape_string($_POST['email']);	
$gender		= $connect->real_escape_string($_POST['gender']);	
$passcode	= $connect->real_escape_string($_POST['passcode']);
$loginwith	= "Google";
$status 	= 1;
/*Check if the user connected before */
$sql = db_query("select * from register_google where passcode='".$passcode."'");
$numrow = mysqli_num_rows($sql);
if($numrow > 0){
$_SESSION['email']	= $email;
$_SESSION['passcode'] 	= $passcode;
header('Location:myaccount.php');
exit();
}else{
$mysql = db_query("insert into register_google set	
firstname	= '".$fname."', 
lastname	= '".$lname."',
email		= '".$email."',
gender		= '".$gender."',
passcode	= '".$passcode."',
loginwith	= '".$loginwith."',
status		= '".$status."'");
			
$_SESSION['email']	= $email;
$_SESSION['passcode'] 	= $passcode;
header('Location:myaccount.php');
exit();
}
}

The MySql table used in this project
CREATE TABLE IF NOT EXISTS `register_google` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(50) DEFAULT NULL,
  `lastname` varchar(50) DEFAULT NULL,
  `email` varchar(60) DEFAULT NULL,
  `gender` varchar(10) DEFAULT NULL,
  `passcode` varchar(100) DEFAULT NULL,
  `profilepic` varchar(250) DEFAULT NULL,
  `loginwith` varchar(10) DEFAULT NULL,
  `status` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
 

Other Login/ Registration Systems

Login/ Register With Facebook
Login/ Register With Twitter
Login/ Register With Linked In
Login/ Register With Yahoo!
Login/ Register With Hotmail
Login/ Register With Email

Top