public function getContentType() { $contentType = 'image/' . $this->image_type; return $contentType; }
public function qrAction() { // 生成二维码 $username = urlencode('账号:') . '*****@*****.**'; $secretKey = 'DPI45HCE'; $url = "otpauth://totp/{$username}?secret={$secretKey}&issuer=" . urlencode('XXTIME.COM'); $qrCode = new QrCode(); $qrCode->setText($url)->setSize(200)->setPadding(10)->setErrorCorrection('low')->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))->setImageType(QrCode::IMAGE_TYPE_PNG); header('Content-Type: ' . $qrCode->getContentType()); $qrCode->render(); exit; // 验证 $totp = new PHPGangsta_GoogleAuthenticator(); $secretKey = $totp->createSecret(32); $oneCode = $totp->getCode($secretKey); $checkResult = $totp->verifyCode($secretKey, $oneCode, 2); // 2 = 2*30sec clock tolerance if ($checkResult) { echo 'OK'; dd($secret, $oneCode); } else { echo 'FAILED'; } exit; }