步骤 1) 创建一个长度为 16 个字符的唯一密码。
PHPGangsta 为 Google Authenticator 提供包装类。您可以使用 composer 下载。
curl -sS https://getcomposer.org/installer | php
php composer.phar require phpgangsta/googleauthenticator:dev-master
Use the below code to generate the secret code.
<?php
require 'vendor/autoload.php';
$authenticator = new PHPGangsta_GoogleAuthenticator();
$secret = $authenticator->createSecret();
echo "Secret: ".$secret;
?>
第 2 步)使用生成的密码创建二维码。
我们需要准备一个使用密码的二维码。如果您想了解更多关于 Google Authenticator 二维码生成的信息。 Github 维基
您可以使用任何二维码生成器来生成二维码,对于这个演示,我使用的是谷歌图表。
require 'vendor/autoload.php';
$authenticator = new PHPGangsta_GoogleAuthenticator();
$secret = $authenticator->createSecret();
echo "Secret: ".$secret."\n"; //save this at server side
$website = 'http://hayageek.com'; //Your Website
$title= 'Hayageek';
$qrCodeUrl = $authenticator->getQRCodeGoogleUrl($title, $secret,$website);
echo $qrCodeUrl;
第 3 步)使用 Google Authenticator App 生成 TOTP(基于时间的一次性密码)
从 Google Play 或 AppStore 下载 Google Authenticator 应用
打开应用程序并点击“+”按钮,然后扫描使用谷歌图表生成的二维码。 Authenticator 应用程序为您的网站生成 TOTP。 TOTP 将每 30 秒更改一次。
使用 Google Authenticator 进行双重身份验证
步骤 4) 在服务器端验证 OTP
require 'vendor/autoload.php';
$authenticator = new PHPGangsta_GoogleAuthenticator();
$secret = '3JMZE4ASZRIISJRI'; //This is used to generate QR code
$otp = '183036' ;//Generated by Authenticator.
$tolerance = 0;
//Every otp is valid for 30 sec.
// If somebody provides OTP at 29th sec, by the time it reaches the server OTP is expired.
//So we can give tolerance =1, it will check current & previous OTP.
// tolerance =2, verifies current and last two OTPS
$checkResult = $authenticator->verifyCode($secret, $otp, $tolerance);
if ($checkResult)
{
echo 'OTP is Validated Succesfully';
} else {
echo 'FAILED';
}
source code refer this link : http://hayageek.com/two-factor-authentication-with-google-authenticator-php/