fastread homefastrread library fastread menu

PHP Tutorial : Login/ Register With Google Open Id

Tutorial by:Admin      Date: 2016-04-14 02:04:46

❰ Previous Next ❱

This article will guide you to crate login/ register with Google.

Lets create a basic registration from with First Name, Last Name, Email and Password. 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 tie then it will redirect to registration page, but it will display first name, last name and email fields. It will not show password field. 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.

You can download the attached file and get the basic html form and other files used in this project. This project required an external file called openid.php, which you will get in the attached zip file.


Include your database connection file and openid.php to you login page.

 require_once 'google/openid.php';  $googleid = new LightOpenID("");
 $googleid->identity = '';
 $googleid->required = array(
$googleid->returnUrl = '';

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

<a href="<?php echo $googleid->authUrl() ?>">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 ($googleid->mode == 'cancel') {
echo "User has canceled authentication !";
} elseif($googleid->validate()) {
$data = $googleid->getAttributes();
$email = $data['contact/email'];
$firstname = $data['namePerson/first'];
$lastname = $data['namePerson/last'];

$Identity = explode("=",$googleid->identity);
$userid = $Identity[1];
$loginwith = 'Google';

$sql = mysql_query("select userid from register where passcode='".$userid."'");
$numrow = mysql_num_rows($sql);
if($numrow > 0){

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.


$fname = mysql_real_escape_string($_POST['fname']);
$lname = mysql_real_escape_string($_POST['lname']);
$email = mysql_real_escape_string($_POST['email']); 
$passcode = mysql_real_escape_string($_POST['passcode']);
$loginwith = mysql_real_escape_string($_POST['loginwith']);
$status = 'active';

$mysql = mysql_query("insert into register set fastname = '".$fname."', 
lastname = '".$lname."',
email = '".$email."',
passcode = '".$passcode."',
loginwith = '".$loginwith."',
status = '".$status."'");


The MySql table used in this project

`userid` int(11) NOT NULL AUTO_INCREMENT,
`fastname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`passcode` varchar(100) NOT NULL,
`loginwith` varchar(20) NOT NULL,
`status` varchar(10) NOT NULL,
PRIMARY KEY (`userid`),
UNIQUE KEY `userid` (`userid`)


❰ Previous Next ❱

PHP Tutorial

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website