直接上實例代碼php
<?php require_once 'PHPGangsta/GoogleAuthenticator.php'; $ga = new PHPGangsta_GoogleAuthenticator(); //建立一個新的"安全密匙SecretKey" //把本次的"安全密匙SecretKey" 入庫,和帳戶關係綁定,客戶端也是綁定這同一個"安全密匙SecretKey" $secret = $ga->createSecret(); echo "安全密匙SecretKey: ".$secret."\n\n"; $qrCodeUrl = $ga->getQRCodeGoogleUrl('www.iamle.com', $secret); //第一個參數是"標識",第二個參數爲"安全密匙SecretKey" 生成二維碼信息 echo "Google Charts URL for the QR-Code: ".$qrCodeUrl."\n\n"; //Google Charts接口 生成的二維碼圖片,方便手機端掃描綁定安全密匙SecretKey //$secret = "xxx";//用戶的安全密匙 $oneCode = $ga->getCode($secret); //服務端計算"一次性驗證碼" echo "服務端計算的驗證碼是:".$oneCode."\n\n"; //把提交的驗證碼和服務端上生成的驗證碼作對比 // $secret 服務端的 "安全密匙SecretKey" // $oneCode 手機上看到的 "一次性驗證碼" // 最後一個參數 爲容差時間,這裏是2 那麼就是 2* 30 sec 一分鐘. // 這裏改爲本身的業務邏輯 $checkResult = $ga->verifyCode($secret, $_GET['code'],0); if ($checkResult) { echo '匹配! OK'; } else { echo '不匹配! FAILED'; }