File: //var/www/test/forgot_password.php
<?php
session_start();
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username']);
$preset_password = trim($_POST['preset_password']);
if (empty($username) || empty($preset_password)) {
$error = "請輸入客戶編號和預設密碼。";
} else {
require_once 'db.php'; // 使用 db.php 連線資料庫
$stmt = $conn->prepare("SELECT email FROM filemaker WHERE user = ?
AND (RIGHT(password, 5) = ? OR RIGHT(idnumber, 5) = ? )");
$stmt->bind_param('sss', $username, $preset_password, $preset_password );
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$user = $result->fetch_assoc();
$email = $user['email'];
if (empty($email)) {
$error = "未找到對應的電子郵件,請聯繫客服。";
} else {
$new_password = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 8);
$hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
$updateStmt = $conn->prepare("UPDATE filemaker SET newpwd = ? WHERE user = ?");
$updateStmt->bind_param('ss', $hashed_password, $username);
if ($updateStmt->execute()) {
$subject = "亞訊寬頻會員密碼重設通知";
$fromEmail = "service@ysnet.com.tw";
$headers = "From: $fromEmail\r\nReply-To: $fromEmail\r\nContent-Type: text/plain; charset=UTF-8\r\n";
$message = "您好,您的新密碼為:$new_password\n請使用此密碼登入,並在登入後記得修改密碼。\n\n亞訊寬頻 客服中心";
if (mail($email, $subject, $message, $headers)) {
$success = "新密碼已發送至您的電子郵件,請檢查信箱。";
header("refresh:2;url=login.php"); // 2 秒後跳轉
} else {
$error = "密碼已變更,但發送通知信失敗,請聯繫客服。";
}
} else {
$error = "密碼更新失敗:" . $conn->error;
}
$updateStmt->close();
}
} else {
$error = "客戶編號或預設密碼錯誤,請重新輸入。";
}
$stmt->close();
$conn->close();
}
}
?>
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>忘記密碼</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f8f8f8;
}
.container {
width: 100%;
max-width: 400px;
background: white;
padding: 20px 30px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
text-align: center;
}
h2 {
margin-bottom: 20px;
font-size: 24px;
}
.input-group {
text-align: center; /* 讓整個區塊置中 */
margin-bottom: 15px;
}
label {
font-weight: bold;
display: block;
margin-bottom: 8px;
}
input {
width: 100%; /* 讓輸入欄位滿版 */
padding: 10px 15px; /* 上下內距10px,左右15px */
margin: 0 auto; /* 水平置中 */
display: block; /* 確保是區塊元素 */
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box; /* 確保 padding 不會影響寬度 */
}
.button-group {
display: flex;
justify-content: space-between;
gap: 10px;
margin-top: 20px;
}
button {
flex: 1;
padding: 10px 0;
border: none;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
}
.reset-btn {
background: #28a745;
color: white;
}
.reset-btn:hover {
background: #218838;
}
.cancel-btn {
background: #dc3545;
color: white;
}
.cancel-btn:hover {
background: #c82333;
}
.message {
margin-top: 15px;
color: red;
}
.success {
color: green;
}
</style>
<script src="/js/iframe-resizer-helper.js"></script>
</head>
<body>
<div class="container">
<h2>忘記密碼</h2>
<form method="POST">
<div class="input-group">
<label for="username">客戶編號</label>
<input type="text" id="username" name="username" required>
</div>
<div class="input-group">
<label for="preset_password">預設密碼 (申請人手機或身份證後 5 碼)</label>
<input type="password" id="preset_password" name="preset_password" required>
</div>
<div class="button-group">
<button type="submit" class="reset-btn">重設密碼</button>
<button type="button" class="cancel-btn" onclick="location.href='login.php';">取消</button>
</div>
<?php if (!empty($error)): ?>
<div class="message"><?php echo $error; ?></div>
<?php elseif (!empty($success)): ?>
<div class="message success"><?php echo $success; ?></div>
<?php endif; ?>
</form>
</div>
</body>
</html>