Here, is the tutorial about to do login with Google account OpenID in PHP. With using minimum line of PHP codes, we can collect required user information from Google, and use this information to register as well as login.
Create Sample Database Table :-
Sample database table with columns id, email, oauth_uid, oauth_provider and username.
CREATE TABLE users
(
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(70),
oauth_uid int(11),
oauth_provider VARCHAR(100),
username VARCHAR(100)
);
Directory structure :-
Google-login //Google logn OpenID library
config
--dbconfig.php
--functions.php
google-open
--openid.php
images
--googlebtn.png
getGoogleData.php
home.php
index.php
login-google.php
logout.php //Logout page
dbconfig.php :-
Connection file to connect with your database.
<?php
define('DB_SERVER', 'dbserver');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
define('USERS_TABLE_NAME', 'users_table_name');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>
Function.php :-
<?php
include 'config/functions.php';
session_start();
if (!empty($_GET['openid_ext1_value_firstname']) && !empty($_GET['openid_ext1_value_lastname']) && !empty($_GET['openid_ext1_value_email'])) {
$username = $_GET['openid_ext1_value_firstname'] . $_GET['openid_ext1_value_lastname'];
$email = $_GET['openid_ext1_value_email'];
$user = new User();
$userdata = $user->checkUserGoogle($uid, 'Google', $username, $email);
if(!empty($userdata)) {
session_start();
$_SESSION['id'] = $userdata['id'];
$_SESSION['oauth_id'] = $uid;
$_SESSION['username'] = $userdata['username'];
$_SESSION['email'] = $userdata['email'];
$_SESSION['oauth_provider'] = $userdata['oauth_provider'];
header("Location: home.php");
} else {
// Something's missing, go back to square 1
header('Location: error.php');
}
}
?>
Index.php Page :-
Add this code in
<?php
session_start();
if (isset($_SESSION['id'])) {
// Redirect to home page as we are already logged in
header("location: home.php");
}
if (array_key_exists("login", $_GET))
{
$oauth_provider = $_GET['oauth_provider'];
if ($oauth_provider == 'google')
{
header("Location: login-google.php");
}
}
?>
//HTML Code
<a href="?login&oauth_provider=google">Google Login</a>
Welcome Page
Name: <?php $_SESSIONS['username'] >
Email: <?php $_SESSIONS['email'] >
Your are logged in with: <?php $_SESSIONS['oauth_provider'] >
<a href="logout.php?logout">Logout</a> from <?php $_SESSIONS['oauth_provider'] >
Create Sample Database Table :-
Sample database table with columns id, email, oauth_uid, oauth_provider and username.
CREATE TABLE users
(
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(70),
oauth_uid int(11),
oauth_provider VARCHAR(100),
username VARCHAR(100)
);
Directory structure :-
Google-login //Google logn OpenID library
config
--dbconfig.php
--functions.php
google-open
--openid.php
images
--googlebtn.png
getGoogleData.php
home.php
index.php
login-google.php
logout.php //Logout page
dbconfig.php :-
Connection file to connect with your database.
<?php
define('DB_SERVER', 'dbserver');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
define('USERS_TABLE_NAME', 'users_table_name');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>
Function.php :-
<?php
include 'config/functions.php';
session_start();
if (!empty($_GET['openid_ext1_value_firstname']) && !empty($_GET['openid_ext1_value_lastname']) && !empty($_GET['openid_ext1_value_email'])) {
$username = $_GET['openid_ext1_value_firstname'] . $_GET['openid_ext1_value_lastname'];
$email = $_GET['openid_ext1_value_email'];
$user = new User();
$userdata = $user->checkUserGoogle($uid, 'Google', $username, $email);
if(!empty($userdata)) {
session_start();
$_SESSION['id'] = $userdata['id'];
$_SESSION['oauth_id'] = $uid;
$_SESSION['username'] = $userdata['username'];
$_SESSION['email'] = $userdata['email'];
$_SESSION['oauth_provider'] = $userdata['oauth_provider'];
header("Location: home.php");
} else {
// Something's missing, go back to square 1
header('Location: error.php');
}
}
?>
Index.php Page :-
Add this code in
<?php
session_start();
if (isset($_SESSION['id'])) {
// Redirect to home page as we are already logged in
header("location: home.php");
}
if (array_key_exists("login", $_GET))
{
$oauth_provider = $_GET['oauth_provider'];
if ($oauth_provider == 'google')
{
header("Location: login-google.php");
}
}
?>
//HTML Code
<a href="?login&oauth_provider=google">Google Login</a>
Welcome Page
Name: <?php $_SESSIONS['username'] >
Email: <?php $_SESSIONS['email'] >
Your are logged in with: <?php $_SESSIONS['oauth_provider'] >
<a href="logout.php?logout">Logout</a> from <?php $_SESSIONS['oauth_provider'] >
0 comments:
Post a Comment