In few months back i had posted an article about Login with facebook account using php, it is most popular on freewebmentor.com. The with minimal line of PHP codes, We can acquire required user information from Google, and use the information to register or login users on click of a button.
Create Sample Database Table
Sample database table with columns id, email, oauth_uid, oauth_provider and username.
1 2 3 4 5 6 7 8 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?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
1 2 3 4 | 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'] > |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: Google, PHP, Web development