Hệ thống đặt lại mật khẩu rất cần thiết cho các trang web có tính năng đăng ký người dùng vì trong trường hợp người dùng quên mật khẩu và không thể đăng nhập thì hệ thống đặt lại mật khẩu này sẽ giúp đặt lại mật khẩu bằng cách gửi liên kết đến địa chỉ email đã đăng ký của họ
Trong hướng dẫn này, chúng tôi sẽ tạo một hệ thống đặt lại mật khẩu bằng PHP. Chắc bạn cũng thích Hệ Thống Xác Minh Tài Khoản Qua Email Sử Dụng PHP
Tham gia với hơn 27.000 thành viên trong nhóm Google của bạn và nhận các hướng dẫn mới nhất
Nhận các hướng dẫn mới nhất của chúng tôi, Hướng dẫn cách thực hiện về phát triển web mỗi ngày ngay trong hộp thư đến của bạn
reset';
$emailBody = 'Hi,
To reset your password, click this link ' . $resetUrl;
$to = $memberListAry[0]["email"];
$subject = 'Reset password';
mail[$to, $subject, $emailBody];
}
public function updatePassword[$id, $password]
{
$hashedPassword = password_hash[$password, PASSWORD_DEFAULT];
$query = 'UPDATE tbl_member SET password = ? WHERE id = ?';
$paramType = 'si';
$paramValue = array[
$hashedPassword,
$id
];
$this->ds->execute[$query, $paramType, $paramValue];
$displayMessage = array[
"status" => "success",
"message" => "Password reset successfully."
];
return $displayMessage;
}
/**
* use this function if you have PHP version 7 or greater
* else use the below fuction generateRandomString
*
* @param int $length
* @param string $keyspace
* @throws \RangeException
* @return string
*/
function getRandomString[int $length = 64, string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ']: string
{
if [$length < 1] {
throw new \RangeException["Length must be a positive integer"];
}
$pieces = [];
$max = mb_strlen[$keyspace, '8bit'] - 1;
for [$i = 0; $i < $length; ++ $i] {
$pieces[] = $keyspace[random_int[0, $max]];
}
return implode['', $pieces];
}
/**
* this generates predictable random strings.
* So the best
* option is to use the above function getRandomString
* and to use that, you need PHP 7 or above
*
* @param number $length
* @return string
*/
function generateRandomString[$length = 10]
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen[$characters];
$randomString = '';
for [$i = 0; $i < $length; $i ++] {
$randomString .= $characters[rand[0, $charactersLength - 1]];
}
return $randomString;
}
}
Đặt lại mật khẩu
/Mô hình/Đặt lại mật khẩu. php
ds = new DataSource[];
}
public function insert[$memberId, $token]
{
$query = 'INSERT INTO tbl_password_reset [member_id, password_recovery_token, expire_at, is_valid] VALUES [?, ?, ?, ?]';
$paramType = 'sssi';
$time = date['Y-m-d H:i:s'];
// expire the token after 12 hours
$RESET_TOKEN_LIFE = '12 hours';
$expireAt = date['Y-m-d H:i:s', strtotime[$time . ' + ' . $RESET_TOKEN_LIFE]];
$paramValue = array[
$memberId,
$token,
$expireAt,
1
];
$memberId = $this->ds->insert[$query, $paramType, $paramValue];
}
public function getMemberForgotByResetToken[$recoveryToken]
{
$query = 'SELECT * FROM tbl_password_reset where password_recovery_token = ? AND is_valid = 1 AND expire_at >= now[]';
$paramType = 's';
$paramValue = array[
$recoveryToken
];
$memberRecord = $this->ds->select[$query, $paramType, $paramValue];
return $memberRecord;
}
public function expireToken[$recoveryToken]
{
$query = 'UPDATE tbl_password_reset SET is_valid = 0, expired_at = now[] WHERE password_recovery_token = ?';
$paramType = 's';
$paramValue = array[
$recoveryToken
];
$this->ds->execute[$query, $paramType, $paramValue];
}
}
Đặt lại mẫu mật khẩu
đặt lại mật khẩu. php
getMemberForgotByResetToken[$token];
if [empty[$memberRecord]] {
// token expired
// do not say that your token has expired for security reasons
// never reveal system state to the end user
echo 'Invalid request!';
exit[];
}
}
if [! empty[$_POST["reset-btn"]]] {
$passwordReset->expireToken[$token];
require_once __DIR__ . '/Model/Member.php';
$member = new Member[];
$displayMessage = $member->updatePassword[$memberRecord[0]['member_id'], $_POST["password"]];
}
?>
Reset Password
Reset Password
Password
Confirm Password
Khôi phục cấu trúc cơ sở dữ liệu mật khẩu
cấu trúc. sql
________số 8Quên email khôi phục mật khẩu
Nếu tên người dùng được tìm thấy trong cơ sở dữ liệu, thư khôi phục sẽ được gửi đến email của người dùng. Thư khôi phục sẽ có một liên kết. Phần thiết yếu của liên kết là mã thông báo bảo mật ngẫu nhiên được băm. Điều này được thiết kế theo cách mà người dùng sẽ không thể bắt chước nó
Một bản ghi đối với thành viên đó sẽ được thực hiện trong bảng tbl_password_reset với mã thông báo bảo mật băm được tạo. Khi người dùng nhấp vào liên kết có mã thông báo, nó sẽ được xác thực thời gian hết hạn
Trong dự án ví dụ này, để cho ngắn gọn, hàm mail[] của Php được sử dụng. Nếu bạn muốn, bạn có thể thay thế nó bằng email SMTP dựa trên PHPMailer