欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)動(dòng)態(tài)口令認(rèn)證的示例代碼

 更新時(shí)間:2024年02月18日 17:05:27   作者:夢(mèng)想oO天堂  
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)動(dòng)態(tài)口令認(rèn)證的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

谷歌身份驗(yàn)證器Google Authenticator是谷歌推出的一款動(dòng)態(tài)口令工具,解決大家各平臺(tái)賬戶遭到惡意攻擊的問(wèn)題,一般在相關(guān)的服務(wù)平臺(tái)登陸中除了用正常用戶名和密碼外,需要再輸入一次谷歌認(rèn)證器生成的動(dòng)態(tài)口令才能驗(yàn)證成功,相當(dāng)于輸入二次密碼,以達(dá)到賬戶的高安全性。

例如交易所、金融平臺(tái)、以及一些錢包等項(xiàng)目等等,都會(huì)使用谷歌身份驗(yàn)證器Google Authenticator來(lái)做二次認(rèn)證,開(kāi)啟谷歌身份驗(yàn)證之后,登錄賬戶,除了輸入用戶名和密碼,還需要輸入谷歌驗(yàn)證器上的動(dòng)態(tài)密碼。谷歌驗(yàn)證器上的動(dòng)態(tài)密碼,也稱為一次性密碼,密碼按照時(shí)間或使用次數(shù)不斷動(dòng)態(tài)變化(默認(rèn) 30 秒變更一次)

代碼參考:https://github.com/PHPGangsta/GoogleAuthenticator

關(guān)鍵代碼:

<?php
// https://github.com/PHPGangsta/GoogleAuthenticator
error_reporting(0);// 關(guān)閉錯(cuò)誤報(bào)告
session_start(); // 啟動(dòng)session  
require_once 'PHPGangsta/GoogleAuthenticator.php';
$ga = new PHPGangsta_GoogleAuthenticator();
// $secret = $ga->createSecret();
// 自定義安全密鑰
$secret = "62H6TMAXQTZBVTRB";
// 手機(jī)端掃描二維碼獲取動(dòng)態(tài)口令
$qrCodeUrl = $ga->getQRCodeGoogleUrl('username', $secret);
echo "二維碼地址: ".$qrCodeUrl."\n\n";
// 輸出動(dòng)態(tài)口令
$oneCode = $ga->getCode($secret);
echo "本次登錄的動(dòng)態(tài)口令:'$oneCode'\n";
// 動(dòng)態(tài)口令認(rè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;
}
?>

使用方法:

手機(jī)端安裝 Microsoft Authenticator

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

將以上代碼生成的二維碼地址在瀏覽器中訪問(wèn)

手機(jī)端掃描二維碼獲取動(dòng)態(tài)驗(yàn)證碼

代碼示例:

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>系統(tǒng)運(yùn)維管理平臺(tái)</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>系統(tǒng)運(yùn)維管理平臺(tái)</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);// 關(guān)閉錯(cuò)誤報(bào)告
session_start(); // 啟動(dòng)session  
require_once 'PHPGangsta/GoogleAuthenticator.php';
$ga = new PHPGangsta_GoogleAuthenticator();
// $secret = $ga->createSecret();
# 自定義安全密鑰
$secret = "62H6TMAXQTZBVTRB";
// $qrCodeUrl = $ga->getQRCodeGoogleUrl('admin', $secret);
// echo "二維碼: ".$qrCodeUrl."\n\n";

// 檢查用戶是否已經(jīng)登錄  
if (isset($_SESSION['username'])) {  
    // 用戶已登錄,顯示用戶信息或其他操作  
    header("Refresh: 3; url=main.php");
} else {  
    if(!isset($_SESSION['num'])){//isset() — 檢測(cè)num變量是否設(shè)置。
        $_SESSION['num'] = 0;
    }
    // 密碼輸入錯(cuò)誤3次,將不允許登錄!
    if($_SESSION['num']<3){
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $username = $_POST['username'];  
            $password = $_POST['password'];  
            //此處應(yīng)該從數(shù)據(jù)庫(kù)中查詢是否存在系統(tǒng)用戶,再進(jìn)行口令驗(yàn)證
            if($username){
                $oneCode = $ga->getCode($secret);
                echo "本次登錄的動(dòng)態(tài)口令:'$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>密碼輸入錯(cuò)誤已超過(guò)3次,系統(tǒng)已不允許登錄!</h1>";
        header("Refresh: 3; url=login.html");
        exit;
    }
}
?>
</div>
</body>
</html>

main.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>系統(tǒng)運(yùn)維管理平臺(tái)</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(); // 啟動(dòng)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>系統(tǒng)運(yùn)維管理平臺(tái)</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實(shí)現(xiàn)動(dòng)態(tài)口令認(rèn)證的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于php動(dòng)態(tài)口令認(rèn)證的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論