Today, We want to share with you PHP Google Multi factor authentication Tutorial With Example from scratch.In this post we will show you enable two factor authentication gmail, hear for Google Multi factor authentication in PHP we will give you demo and example for implement.In this post, we will learn about php 2 factor authentication google authenticator with an example.
PHP Google Multi factor authentication Tutorial With Example from scratch
There are the Following The simple About PHP Google Multi factor authentication Tutorial With Example from scratch Full Information With Example and source code.
As I will cover this Post with live Working example to develop google 2 factor authentication new phone, so the 2 factor authentication php for this example is following below.
Step 1: Google Authenticator Application Install
Free Download and Install Google Authenticator Application:
Step 2: Database Configuration:
store Google Secret Code.
CREATE TABLE `members` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `membername` varchar(50) NOT NULL, `email` varchar(100) NOT NULL, `password` varchar(250) NOT NULL, `live_code_key_google_secret` varchar(250) NOT NULL, PRIMARY KEY (`id`) );
Step 3: Database Connection Script:
app_config/database_config.php
PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => FALSE, ); $dsn = 'mysql:host=' . HOST . ';dbname=' . DATABASE . ';charset=' . CHARSET; $instance = new PDO($dsn, USER, PASSWORD, $opt); } return $instance; } ?>
Step 4: SignUp Page:
signup.php
SignUp - Google Multi factor authentication in PHP Demo: Using Google Two factor authentication in PHP
Note: This is demo version from Pakainfo Empires tutorials. (Multi-factor Authentication)
SignUp
<?php if ($signup_error_message != "") { echo 'Error: ' . $signup_error_message . ''; } ?>Click here to SignIn if you have already registred your account.
Step 5: SignIn Page:
index.php
SignIn - Google Multi factor authentication in PHP Demo google authentication php: Using Google Two factor authentication in PHP
Note: This is demo version from Pakainfo Empires tutorials. (Multi-factor Authentication)
SignIn
<?php if ($login_error_message != "") { echo 'Error: ' . $login_error_message . ''; } ?>Not Registered Yet? SignUp Here
Step 6: Create Library file:
googleauthlibs/googleauthlibs.php
db = $db; } function __destruct() { $this->db = null; } public function SignUp($name, $email, $membername, $password, $live_code_key_google_secret) { $sql_query = $this->db->prepare("INSERT INTO members(name, email, membername, password, live_code_key_google_secret) VALUES (:name,:email,:membername,:password,:live_code_key_google_secret)"); $sql_query->bindParam("name", $name, PDO::PARAM_STR); $sql_query->bindParam("email", $email, PDO::PARAM_STR); $sql_query->bindParam("membername", $membername, PDO::PARAM_STR); $hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => 10]); $sql_query->bindParam("password", $hash, PDO::PARAM_STR); $sql_query->bindParam("live_code_key_google_secret", $live_code_key_google_secret, PDO::PARAM_STR); $sql_query->execute(); return $this->db->lastInsertId(); } public function isMembername($membername) { $sql_query = $this->db->prepare("SELECT id FROM members WHERE membername=:membername"); $sql_query->bindParam("membername", $membername, PDO::PARAM_STR); $sql_query->execute(); if ($sql_query->rowCount() > 0) { return true; } else { return false; } } public function isEmail($email) { $sql_query = $this->db->prepare("SELECT id FROM members WHERE email=:email"); $sql_query->bindParam("email", $email, PDO::PARAM_STR); $sql_query->execute(); if ($sql_query->rowCount() > 0) { return true; } else { return false; } } public function SignIn($membername, $password) { $sql_query = $this->db->prepare("SELECT id, password FROM members WHERE membername=:membername OR email=:email"); $sql_query->bindParam("membername", $membername, PDO::PARAM_STR); $sql_query->bindParam("email", $membername, PDO::PARAM_STR); $sql_query->execute(); if ($sql_query->rowCount() > 0) { $result = $sql_query->fetch(PDO::FETCH_OBJ); $enc_password = $result->password; if (password_verify($password, $enc_password)) { return $result->id; } else { return false; } } else { return false; } } public function MemberDetails($member_id) { $sql_query = $this->db->prepare("SELECT id, name, membername, email, live_code_key_google_secret FROM members WHERE id=:member_id"); $sql_query->bindParam("member_id", $member_id, PDO::PARAM_STR); $sql_query->execute(); if ($sql_query->rowCount() > 0) { return $sql_query->fetch(PDO::FETCH_OBJ); } } }
Step 7: Member SignUp:
signup.php
createSecret(); $signup_error_message = ''; // check SignUp request if (!empty($_POST['btnRegister'])) { if ($_POST['name'] == "") { $signup_error_message = 'Name field is required!'; } else if ($_POST['email'] == "") { $signup_error_message = 'Email field is required!'; } else if ($_POST['membername'] == "") { $signup_error_message = 'Membername field is required!'; } else if ($_POST['password'] == "") { $signup_error_message = 'Password field is required!'; } else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $signup_error_message = 'Invalid email address!'; } else if ($app->isEmail($_POST['email'])) { $signup_error_message = 'Email is already in use!'; } else if ($app->isMembername($_POST['membername'])) { $signup_error_message = 'Membername is already in use!'; } else { $member_id = $app->SignUp($_POST['name'], $_POST['email'], $_POST['membername'], $_POST['password'], $secret); $_SESSION['member_id'] = $member_id; header("Location: confirm_google_auth.php"); } } ?>
Step 8: Member Device Conformation:
confirm_google_auth.php
MemberDetails($_SESSION['member_id']); require_once __DIR__ . '/GoogleAuthenticator/GoogleAuthenticator.php'; $PHPGoogleAuth = new PHPGangsta_GoogleAuthenticator(); $qr_code = $PHPGoogleAuth->getQRCodeGoogleUrl($member->email, $member->live_code_key_google_secret, 'pakainfo.com'); $live_msg_error_alert = ''; if (isset($_POST['validateBtn'])) { $code = $_POST['code']; if ($code == "") { $live_msg_error_alert = 'Please Scan above QR code to configure your application and enter genereated authentication code to validated!'; } else { if($PHPGoogleAuth->verifyCode($member->live_code_key_google_secret, $code, 2)) { header("Location: profile.php"); } else { $live_msg_error_alert = 'Invalid Authentication Code!'; } } } ?>Confirm Member Device Demo: Using Google Two factor authentication in PHP
Note: This is demo version from Pakainfo Empires tutorials. (Multi-factor Authentication)
Application Authentication
Please download and install Google authenticate app on your phone, and scan following QR code to configure your device.
<img src="">Click here to SignIn if you have already signuped your account.
Step 9: Create profile page:
profile.php
MemberDetails($_SESSION['member_id']); ?>Member Profile Demo: Using Google Two factor authentication in PHP
Note: This is demo version from Pakainfo Empires tutorials. (Multi-factor Authentication)
Member Profile
Welcome name; ?>
Account Details:
Name: name; ?>
Membername membername; ?>
Email email; ?>
Click here to Logout
Step 10: SignIn Script:
index.php
SignIn($membername, $password); if($member_id > 0) { $_SESSION['member_id'] = $member_id; header("Location: validate_login.php"); } else { $login_error_message = 'Invalid login details!'; } } } ?>
Step 11: Validated Security Code:
validate_login.php
MemberDetails($_SESSION['member_id']); require_once __DIR__ . '/GoogleAuthenticator/GoogleAuthenticator.php'; $PHPGoogleAuth = new PHPGangsta_GoogleAuthenticator(); $live_msg_error_alert = ''; if (isset($_POST['validateBtn'])) { $code = $_POST['code']; if ($code == "") { $live_msg_error_alert = 'Please enter authentication code to validated!'; } else { if($PHPGoogleAuth->verifyCode($member->live_code_key_google_secret, $code, 2)) { // success header("Location: profile.php"); } else { // fail $live_msg_error_alert = 'Invalid Authentication Code!'; } } } ?>Validate SignIn Demo: Using Google Two factor authentication in PHP
Note: This is demo version from Pakainfo Empires tutorials. (Multi-factor Authentication)
Multi Factor Authentication
Click here to SignIn if you have already signuped your account.
Step 12: Member Logout:
logout.php
Angular 6 CRUD Operations Application Tutorials
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about PHP Google Multi factor authentication Tutorial With Example from scratch.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.