• PHP版谷歌验证 (Google Authenticator)


    直接上实例代码

    <?php
    
    require_once 'PHPGangsta/GoogleAuthenticator.php';
    
    $ga = new PHPGangsta_GoogleAuthenticator();
    
    //创建一个新的"安全密匙SecretKey"
    //把本次的"安全密匙SecretKey" 入库,和账户关系绑定,客户端也是绑定这同一个"安全密匙SecretKey"
    $secret = $ga->createSecret();
    echo "安全密匙SecretKey: ".$secret."
    
    ";
    
    $qrCodeUrl = $ga->getQRCodeGoogleUrl('www.iamle.com', $secret); //第一个参数是"标识",第二个参数为"安全密匙SecretKey" 生成二维码信息
    echo "Google Charts URL for the QR-Code: ".$qrCodeUrl."
    
    "; //Google Charts接口 生成的二维码图片,方便手机端扫描绑定安全密匙SecretKey
    //$secret = "xxx";//用户的安全密匙
    $oneCode = $ga->getCode($secret); //服务端计算"一次性验证码"
    echo "服务端计算的验证码是:".$oneCode."
    
    ";
    
    //把提交的验证码和服务端上生成的验证码做对比
    // $secret 服务端的 "安全密匙SecretKey"
    // $oneCode 手机上看到的 "一次性验证码"
    // 最后一个参数 为容差时间,这里是2 那么就是 2* 30 sec 一分钟.
    // 这里改成自己的业务逻辑
    $checkResult = $ga->verifyCode($secret, $_GET['code'],0);
    if ($checkResult) {
        echo '匹配! OK';
    } else {
        echo '不匹配! FAILED';
    }

    giuhub:https://github.com/PHPGangsta/GoogleAuthenticator

  • 相关阅读:
    php实现rpc简单的方法
    统计代码量
    laravel的速查表
    header的参数不能带下划线
    PHP简单实现单点登录功能示例
    phpStorm函数注释的设置
    数据结构基础
    laravel生命周期和核心思想
    深入理解php底层:php生命周期
    Jmeter:实例(性能测试目标)
  • 原文地址:https://www.cnblogs.com/ygcool/p/8528349.html
Copyright © 2020-2023  润新知