Linuxword Global
当前位置: 建站相关 > PHP设置谷歌验证器(Google Authenticator)实现操作2FA二步验证

**使用说明:**开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码。实现Google Authenticator功能需要服务器端和客户端的支持。服务器端负责密钥的生成、验证一次性密码是否正确。客户端记录密钥后生成一次性密码。
下载谷歌验证类库文件放到项目合适位置(我这边放在项目Vender下面)
下载地址: https://github.com/PHPGangsta/GoogleAuthenticator

//引入谷歌验证器类
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标签里面即可,示例图展示如下:

images-2

<?php
require_once 'PHPGangsta/GoogleAuthenticator.php';

$ga = new PHPGangsta_GoogleAuthenticator();
$secret = $ga->createSecret();
echo "Secret is: ".$secret."\n\n";

$qrCodeUrl = $ga->getQRCodeGoogleUrl('Blog', $secret);
echo "Google Charts URL for the QR-Code: ".$qrCodeUrl."\n\n";

$oneCode = $ga->getCode($secret);
echo "Checking Code '$oneCode' and Secret '$secret':\n";

$checkResult = $ga->verifyCode($secret, $oneCode, 2); // 2 = 2*30sec clock tolerance
if ($checkResult) {
echo 'OK';
} else {
echo 'FAILED';
}

 

「梦想一旦被付诸行动,就会变得神圣,如果觉得我的文章对您有用,请帮助本站成长」

赞(0) *** 在线联系博主***
+1(323)529-5889

TeleGram联系

WhatsAPP联系

上一篇:

下一篇:

相关推荐

博客简介

本站CDN采用VmShell免费提供离中国大陆最近的香港CMI高速网络做支撑,ToToTel打造全球最快速的边沿网络支撑服务,具体详情请见 :https://vmshell.com/ 以及 https://tototel.com/,网站所有的文件和内容禁止大陆网站搬迁复制,谢谢,VPS营销投稿邮箱: admin@linuxxword.com,我们免费帮大家发布,不收取任何费用,请提供完整测试文稿!

精彩评论

关于VmShell INC

VMSHELL INC 是一家成立于2021年的美国云计算服务公司,总部位于怀俄明州谢里丹,专注于提供全球数据中心的虚拟机服务器租赁和全球金融服务。 公司旗下品牌包括VmShell 和ToToTel,业务覆盖亚洲和美洲以及欧洲,致力于为外贸企业提供高效、稳定的网络解决方案。购买CMI香港服务器

站点统计

  • 文章总数: 3140 篇
  • 草稿数目: 0 篇
  • 分类数目: 9 个
  • 独立页面: 0 个
  • 评论总数: 2 条
  • 链接总数: 0 个
  • 标签总数: 48026 个
  • 注册用户: 139 人
  • 访问总量: 8,650,586 次
  • 最近更新: 2025年12月5日