php实现动态口令认证的示例代码

 更新时间:2024年02月18日 17:05:27   作者:梦想oO天堂  
这篇文章主要为大家详细介绍了php实现动态口令认证的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

谷歌身份验证器Google Authenticator是谷歌推出的一款动态口令工具,解决大家各平台账户遭到恶意攻击的问题,一般在相关的服务平台登陆中除了用正常用户名和密码外,需要再输入一次谷歌认证器生成的动态口令才能验证成功,相当于输入二次密码,以达到账户的高安全性。

例如交易所、金融平台、以及一些钱包等项目等等,都会使用谷歌身份验证器Google Authenticator来做二次认证,开启谷歌身份验证之后,登录账户,除了输入用户名和密码,还需要输入谷歌验证器上的动态密码。谷歌验证器上的动态密码,也称为一次性密码,密码按照时间或使用次数不断动态变化(默认 30 秒变更一次)

代码参考:https://github.com/PHPGangsta/GoogleAuthenticator

关键代码:

<?php
// https://github.com/PHPGangsta/GoogleAuthenticator
error_reporting(0);// 关闭错误报告
session_start(); // 启动session  
require_once 'PHPGangsta/GoogleAuthenticator.php';
$ga = new PHPGangsta_GoogleAuthenticator();
// $secret = $ga->createSecret();
// 自定义安全密钥
$secret = "62H6TMAXQTZBVTRB";
// 手机端扫描二维码获取动态口令
$qrCodeUrl = $ga->getQRCodeGoogleUrl('username', $secret);
echo "二维码地址: ".$qrCodeUrl."\n\n";
// 输出动态口令
$oneCode = $ga->getCode($secret);
echo "本次登录的动态口令:'$oneCode'\n";
// 动态口令认证
$checkResult = $ga->verifyCode($secret, $password,2);    // 2 = 2*30sec clock tolerance
if ($checkResult) {
    $_SESSION['username'] = $username;
    echo "<h1>登录成功!</h1>";
    header("Refresh: 5; url=main.php");
    exit;
} else {
    echo "<h1>登录失败!</h1>";
    header("Refresh: 3; url=login.html");
    exit;
}
?>

使用方法:

手机端安装 Microsoft Authenticator

下载地址:https://www.microsoft.com/en-us/security/mobile-authenticator-app

将以上代码生成的二维码地址在浏览器中访问

手机端扫描二维码获取动态验证码

代码示例:

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>系统运维管理平台</title>
    <link rel="stylesheet" type="text/css" href="login.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" />
</head>
<body>
    <div id="login">
        <h1>Login</h1>
        <form method="post" action="login.php">
            <input type="text" required="required" placeholder="用户名" name="username"></input>
            <input type="password" required="required" placeholder="密码" name="password"></input>
            <button class="but" type="submit">登录</button>
        </form>
    </div>
</body>
</html>

login.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>系统运维管理平台</title>
    <link rel="stylesheet" type="text/css" href="login.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" />
</head>
<body>
<div id="login">
<?php
// https://github.com/PHPGangsta/GoogleAuthenticator
error_reporting(0);// 关闭错误报告
session_start(); // 启动session  
require_once 'PHPGangsta/GoogleAuthenticator.php';
$ga = new PHPGangsta_GoogleAuthenticator();
// $secret = $ga->createSecret();
# 自定义安全密钥
$secret = "62H6TMAXQTZBVTRB";
// $qrCodeUrl = $ga->getQRCodeGoogleUrl('admin', $secret);
// echo "二维码: ".$qrCodeUrl."\n\n";

// 检查用户是否已经登录  
if (isset($_SESSION['username'])) {  
    // 用户已登录,显示用户信息或其他操作  
    header("Refresh: 3; url=main.php");
} else {  
    if(!isset($_SESSION['num'])){//isset() — 检测num变量是否设置。
        $_SESSION['num'] = 0;
    }
    // 密码输入错误3次,将不允许登录!
    if($_SESSION['num']<3){
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $username = $_POST['username'];  
            $password = $_POST['password'];  
            //此处应该从数据库中查询是否存在系统用户,再进行口令验证
            if($username){
                $oneCode = $ga->getCode($secret);
                echo "本次登录的动态口令:'$oneCode'\n";
                $checkResult = $ga->verifyCode($secret, $password,2);    // 2 = 2*30sec clock tolerance
                if ($checkResult) {
                    $_SESSION['username'] = $username;
                    echo "<h1>登录成功!</h1>";
                    header("Refresh: 5; url=main.php");
                    exit;
                } else {
                    $_SESSION['num']++;
                    echo "<h1>登录失败!</h1>";
                    header("Refresh: 3; url=login.html");
                    exit;
                }
            }else{
                echo "<h1>登录失败!</h1>";
                header("Refresh: 3; url=login.html");
                exit;
            }
        } else {  
            header("Location: login.html");
            exit; 
        }
    }else{
        echo "<h1>密码输入错误已超过3次,系统已不允许登录!</h1>";
        header("Refresh: 3; url=login.html");
        exit;
    }
}
?>
</div>
</body>
</html>

main.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>系统运维管理平台</title>
    <link rel="stylesheet" type="text/css" href="login.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" />
</head>
<body>
    <div id="login">
    <?php
    session_start(); // 启动session 
    if (isset($_SESSION['username'])) {  
        echo "<h2>".$_SESSION['username']."您已登录!</h2>";
        echo "<h2><a href='logout.php'>退出登录</a></h2>";
    } else{
        header("Refresh: 3; url=login.html");
    }
    ?>
</body>
</html>

logout.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>系统运维管理平台</title>
    <link rel="stylesheet" type="text/css" href="login.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" />
</head>
<body>
    <div id="login">
    <?php
        session_start();
        if(isset($_SESSION['username']))
        {
            session_destroy();
        }
        header("Refresh: 3; url=login.html");
    ?>
</body>
</html>

login.css

html{   
    width: 100%;   
    height: 100%;   
    overflow: hidden;   
    font-style: sans-serif;   
}   
body{   
    width: 100%;   
    height: 100%;   
    font-family: 'Open Sans',sans-serif;   
    margin: 0;   
    background-color: #4A374A;   
}   
#login{   
    position: absolute;   
    top: 50%;   
    left:50%;   
    margin: -150px 0 0 -150px;   
    width: 300px;   
    height: 300px;   
}   
#login h1,h2{   
    color: #fff;   
    /* text-shadow:0 0 10px;    */
    letter-spacing: 1px;   
    text-align: center;   
}   
h1,h2{   
    font-size: 2em;   
    margin: 0.67em 0;   
}   
input{   
    width: 278px;   
    height: 18px;   
    margin-bottom: 10px;   
    outline: none;   
    padding: 10px;   
    font-size: 13px;   
    color: #fff;   
    /* text-shadow:1px 1px 1px;    */
    border-top: 1px solid #312E3D;   
    border-left: 1px solid #312E3D;   
    border-right: 1px solid #312E3D;   
    border-bottom: 1px solid #56536A;   
    border-radius: 4px;   
    background-color: #2D2D3F;   
}   
.but{   
    width: 300px;   
    min-height: 20px;   
    display: block;   
    background-color: #4a77d4;   
    border: 1px solid #3762bc;   
    color: #fff;   
    padding: 9px 14px;   
    font-size: 15px;   
    line-height: normal;   
    border-radius: 5px;   
    margin: 0;   
}

以上就是php实现动态口令认证的示例代码的详细内容,更多关于php动态口令认证的资料请关注脚本之家其它相关文章!

相关文章

  • PHP预定义接口——Iterator用法示例

    PHP预定义接口——Iterator用法示例

    这篇文章主要介绍了PHP预定义接口——Iterator用法,结合实例形式分析了PHP Iterator(迭代器)接口相关原理、定义与使用方法,需要的朋友可以参考下
    2020-06-06
  • PHP结合Redis+MySQL实现冷热数据交换应用案例详解

    PHP结合Redis+MySQL实现冷热数据交换应用案例详解

    这篇文章主要介绍了PHP结合Redis+MySQL实现冷热数据交换应用案例,结合具体实例形式详细分析了Redis+MySQL冷热数据交换原理、实现方法及相关操作技巧,需要的朋友可以参考下
    2019-07-07
  • PHP 8新特性简介

    PHP 8新特性简介

    这篇文章主要介绍了php8新特性的相关资料,帮助大家决定是否要升级新版本,感兴趣的朋友可以了解下
    2020-08-08
  • PHP实现登陆表单提交CSRF及验证码

    PHP实现登陆表单提交CSRF及验证码

    本文主要介绍了PHP实现登陆表单提交CSRF及验证码的方法。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • PHP 删除一个目录及目录下的所有文件的函数代码

    PHP 删除一个目录及目录下的所有文件的函数代码

    PHP删除一个目录及目录下的文件代码,即删除目录或删除文件。
    2010-05-05
  • PHP 文件上传功能实现代码

    PHP 文件上传功能实现代码

    经过了几个小时的苦苦搜索,终于把这个问题给解决了. php文件上传对于高手来说确实是小菜,可是对我新手来说就显得手足无措了.
    2009-06-06
  • 解析PHP的session过期设置

    解析PHP的session过期设置

    本篇文章是对PHP的session过期设置进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • php获取文件名后缀常用方法小结

    php获取文件名后缀常用方法小结

    这篇文章主要介绍了php获取文件名后缀常用方法,实例分析了五种常用的php获取文件名后缀的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-02-02
  • 基于PHP实现敏感词过滤功能

    基于PHP实现敏感词过滤功能

    后端同学在做一些社区论坛类型项目时候,可能会绕不开敏感词的过滤这个功能,特别是在微信小程序中,如果主营类目被定义为【社交】 那么敏感词、图片、视频的各种过滤功能是逃不掉的,否则是无法上线的,下面就以PHP代码为例,分析一下这个功能的具体实现
    2023-10-10
  • php源码分析之DZX1.5加密解密函数authcode用法

    php源码分析之DZX1.5加密解密函数authcode用法

    这篇文章主要介绍了php源码分析之DZX1.5加密解密函数authcode用法,实例分析了DZX1.5中authcode函数加密与解密的使用技巧,需要的朋友可以参考下
    2015-06-06

最新评论