Login with Google Account OpenID
Contact Me For Sample FileDatabase
Sample database users table 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)
);
(
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(70),
oauth_uid int(11),
oauth_provider VARCHAR(100),
username VARCHAR(100)
);
In this tutorial we have the following directory structure
google-open //Google LightOpenID library
config
-- functions.php
-- dbconfig.php //Database connection
index.php
home.php
getGoogleData.php //The Callback URL file
login-google.php
config
-- functions.php
-- dbconfig.php //Database connection
index.php
home.php
getGoogleData.php //The Callback URL file
login-google.php
dbconfig.php
Database configuration file.
<?php
define('DB_SERVER', 'dbserver');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
define('USERS_TABLE_NAME', 'users_table_name'); //Replace your users table name here
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) ordie(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>
define('DB_SERVER', 'dbserver');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
define('USERS_TABLE_NAME', 'users_table_name'); //Replace your users table name here
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) ordie(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>
login-google.php
In root directory find out the below line at login-google.php code and replaceyourdomain.com with your own domain value.
define('CALLBACK_URL', 'http://yourdomain.com/getGoogleData.php');
index.php
If you want to modify your web project existing login or index pages, just use following code.
<?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>
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>
home.php
In home page you can display user details by accessing session variables.
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 :