php實(shí)現(xiàn)動(dòng)態(tài)口令認(rèn)證的示例代碼
谷歌身份驗(yàn)證器Google Authenticator是谷歌推出的一款動(dòng)態(tài)口令工具,解決大家各平臺(tái)賬戶遭到惡意攻擊的問題,一般在相關(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來做二次認(rèn)證,開啟谷歌身份驗(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
將以上代碼生成的二維碼地址在瀏覽器中訪問
手機(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ù)庫中查詢是否存在系統(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>登錄失?。?lt;/h1>";
header("Refresh: 3; url=login.html");
exit;
}
}else{
echo "<h1>登錄失?。?lt;/h1>";
header("Refresh: 3; url=login.html");
exit;
}
} else {
header("Location: login.html");
exit;
}
}else{
echo "<h1>密碼輸入錯(cuò)誤已超過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)文章
PHP獲取數(shù)組表示的路徑方法分析【數(shù)組轉(zhuǎn)字符串】
這篇文章主要介紹了PHP獲取數(shù)組表示的路徑,結(jié)合實(shí)例形式對(duì)比分析了數(shù)組轉(zhuǎn)字符串的實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09
PHP消息隊(duì)列實(shí)現(xiàn)及應(yīng)用詳解【隊(duì)列處理訂單系統(tǒng)和配送系統(tǒng)】
這篇文章主要介紹了PHP消息隊(duì)列實(shí)現(xiàn)及應(yīng)用,結(jié)合實(shí)例形式詳細(xì)分析了php消息隊(duì)列的概念、原理及隊(duì)列處理訂單系統(tǒng)和配送系統(tǒng)案例,需要的朋友可以參考下2019-05-05
php使用socket簡單實(shí)現(xiàn)通信功能
socket只不過是一個(gè)數(shù)據(jù)結(jié)構(gòu)。使用這個(gè)socket數(shù)據(jù)結(jié)構(gòu)去開始一個(gè)客戶端和服務(wù)器之間的會(huì)話。服務(wù)器是一直在監(jiān)聽準(zhǔn)備產(chǎn)生一個(gè)新的會(huì)話。當(dāng)一個(gè)客戶端連接服務(wù)器,它就打開服務(wù)器正在進(jìn)行監(jiān)聽的一個(gè)端口進(jìn)行會(huì)話2023-03-03
php實(shí)現(xiàn)事件監(jiān)聽與觸發(fā)的方法
這篇文章主要介紹了php實(shí)現(xiàn)事件監(jiān)聽與觸發(fā)的方法,可實(shí)現(xiàn)時(shí)間的綁定、觸發(fā)與注銷等功能,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11
PHP中數(shù)據(jù)類型轉(zhuǎn)換的三種方式
這篇文章主要介紹了PHP中數(shù)據(jù)類型轉(zhuǎn)換的三種方式,本文講解了允許轉(zhuǎn)換的PHP數(shù)據(jù)類型、PHP數(shù)據(jù)類型的三種轉(zhuǎn)換方式等內(nèi)容,需要的朋友可以參考下2015-04-04

