使用說明:開啟Google的登陸二步驗證(即Google Authenticator服務)后用戶登陸時需要輸入額外由手機客戶端生成的一次性密碼。實現Google Authenticator功能需要服務器端和客戶端的支持。服務器端負責密鑰的生成、驗證一次性密碼是否正確。客戶端記錄密鑰后生成一次性密碼。
下載谷歌驗證類庫文件放到項目合適位置(我這邊放在項目Vender下面)
https://github.com/PHPGangsta/GoogleAuthenticator
PHP代碼示例:
//引入谷歌驗證器類 vendor('googleAuth.GoogleAuthenticator-master.PHPGangsta.GoogleAuthenticator'); $ga = new \PHPGangsta_GoogleAuthenticator(); //這是生成的密鑰,每個用戶唯一一個,為用戶保存起來用於驗證 $secret = $ga->createSecret(); //echo $secret; //下面為生成二維碼,內容是一個URI地址(otpauth://totp/賬號?secret=密鑰&issuer=標題) $qrCodeUrl = $ga->getQRCodeGoogleUrl('luokakale', $secret, 'googleVerify'); //echo $qrCodeUrl;
將上面生成的二維碼地址放入網頁img標簽里面即可,示例圖展示如下:
接下來就是客戶端谷歌驗證APP掃碼綁定后進行輸碼驗證,驗證PHP代碼示例:
//引入谷歌驗證器類 vendor('googleAuth.GoogleAuthenticator-master.PHPGangsta.GoogleAuthenticator'); $ga = new \PHPGangsta_GoogleAuthenticator(); //下面為驗證參數 $code = $_GET['code'];//客戶提交上來的谷歌驗證APP里面對應的驗證碼 //該用戶綁定谷歌驗證生成的唯一秘鑰 $secret = 'VO2WA6NG3XZZEU4E'; //驗證用戶提交的驗證碼是否正確 $checkResult = $ga->verifyCode($secret, $code, 1); if ($checkResult) { echo 'SUCCESS'; } else { echo 'FAILED'; }
驗證成功即客戶綁定谷歌驗證成功。該谷歌驗證可用於客戶登陸支付個人設置等各種場景。
本文屬原創內容,為了尊重他人勞動,轉載請注明本文地址:
https://www.cnblogs.com/luokakale/p/11384838.html