0%

[技能檢定]題組二 步驟8 建置忘記密碼功能

題組的密碼尋回功能是以明碼方式在前台顯示出來,實務上千萬不要這麼做,這裏只是在測驗應檢人知道資料庫的應用方式。

建立密碼尋回畫面

./front/forget.php 畫面html碼

1
2
3
4
5
6
7
8
9
10
11
<fieldset>
<legend>忘記密碼</legend>
<div>請輸入信箱以查詢密碼</div>
<div>
<input type="text" name="email" id="email">
</div>
<div id="result"></div>
<div>
<button onclick="forget()">
</div>
</fieldset>

撰寫ajax取回結果函式

/view/front/forgot.php

1
2
3
4
5
function forget(){
$.get("./api/forget.php",{email:$("#email").val()},(res)=>{
$("#result").text(res)
})
}

撰寫後端回傳密碼程式

1
2
3
4
5
6
7
8
9
10
11
12
include_once "db.php";
//根據郵件來取得使用者資料
$user=$User->find(['email'=>$_GET['email']]);

//判斷是否有取得使用者資料
if(empty($user)){
//回傳查無使用者的狀況
echo "查無此資料";
}else{
//回傳使用者密碼
echo "您的密碼為:".$user['pw'];
}