php實現(xiàn)動態(tài)口令認證的示例代碼
谷歌身份驗證器Google Authenticator是谷歌推出的一款動態(tài)口令工具,解決大家各平臺賬戶遭到惡意攻擊的問題,一般在相關的服務平臺登陸中除了用正常用戶名和密碼外,需要再輸入一次谷歌認證器生成的動態(tài)口令才能驗證成功,相當于輸入二次密碼,以達到賬戶的高安全性。
例如交易所、金融平臺、以及一些錢包等項目等等,都會使用谷歌身份驗證器Google Authenticator來做二次認證,開啟谷歌身份驗證之后,登錄賬戶,除了輸入用戶名和密碼,還需要輸入谷歌驗證器上的動態(tài)密碼。谷歌驗證器上的動態(tài)密碼,也稱為一次性密碼,密碼按照時間或使用次數不斷動態(tài)變化(默認 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";
// 手機端掃描二維碼獲取動態(tài)口令
$qrCodeUrl = $ga->getQRCodeGoogleUrl('username', $secret);
echo "二維碼地址: ".$qrCodeUrl."\n\n";
// 輸出動態(tài)口令
$oneCode = $ga->getCode($secret);
echo "本次登錄的動態(tài)口令:'$oneCode'\n";
// 動態(tài)口令認證
$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
將以上代碼生成的二維碼地址在瀏覽器中訪問
手機端掃描二維碼獲取動態(tài)驗證碼
代碼示例:
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>系統(tǒng)運維管理平臺</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)運維管理平臺</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'];
//此處應該從數據庫中查詢是否存在系統(tǒng)用戶,再進行口令驗證
if($username){
$oneCode = $ga->getCode($secret);
echo "本次登錄的動態(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>密碼輸入錯誤已超過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)運維管理平臺</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>系統(tǒng)運維管理平臺</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實現(xiàn)動態(tài)口令認證的示例代碼的詳細內容,更多關于php動態(tài)口令認證的資料請關注腳本之家其它相關文章!
相關文章
PHP消息隊列實現(xiàn)及應用詳解【隊列處理訂單系統(tǒng)和配送系統(tǒng)】
這篇文章主要介紹了PHP消息隊列實現(xiàn)及應用,結合實例形式詳細分析了php消息隊列的概念、原理及隊列處理訂單系統(tǒng)和配送系統(tǒng)案例,需要的朋友可以參考下2019-05-05
php實現(xiàn)事件監(jiān)聽與觸發(fā)的方法
這篇文章主要介紹了php實現(xiàn)事件監(jiān)聽與觸發(fā)的方法,可實現(xiàn)時間的綁定、觸發(fā)與注銷等功能,具有一定的參考借鑒價值,需要的朋友可以參考下2014-11-11

