File: //proc/self/cwd/newpwd.php
<?php
session_start();
if (!isset($_SESSION['user'])) {
header('Location: login.php');
exit();
}
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_SESSION['user'];
$old_password = trim($_POST['old_password']);
$new_password = trim($_POST['new_password']);
$confirm_password = trim($_POST['confirm_password']);
function isValidPassword($password) {
return preg_match('/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/', $password);
}
require_once 'db.php'; // 使用 db.php 連線資料庫
$stmt = $conn->prepare("SELECT newpwd, email FROM filemaker WHERE user = ?");
$stmt->bind_param('s', $username);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
if (!$user || !password_verify($old_password, $user['newpwd'])) {
$error = "舊密碼錯誤,請重新輸入。";
} elseif (!isValidPassword($new_password)) {
$error = "新密碼長度需至少 8 碼,且需包含英文字母與數字。";
} elseif ($new_password === $old_password) {
$error = "新密碼不能與舊密碼相同,請重新輸入。";
}elseif ($new_password !== $confirm_password) {
$error = "新密碼與確認密碼不一致,請重新輸入。";
} else {
$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()) {
$email = $user['email'];
$subject = "亞訊寬頻會員帳號密碼通知";
$fromEmail = "service@ysnet.com.tw";
$headers = "From: $fromEmail\r\nReply-To: $fromEmail\r\nContent-Type: text/plain; charset=UTF-8\r\n";
$message = "親愛的用戶您好,\n\n您的寬頻會員密碼已成功更新。\n您的新密碼:$new_password 。\n請使用新密碼登入。\n\n亞訊寬頻 客服中心";
if (mail($email, $subject, $message, $headers)) {
$success = "密碼修改成功,請使用新密碼登入。";
} else {
$error = "密碼已變更,但發送通知信失敗,請聯繫客服。";
}
$success = "密碼修改成功,請重新登入。";
session_destroy();
header("refresh:1;url=login.php");
} else {
$error = "密碼更新失敗:" . $conn->error;
}
$updateStmt->close();
}
$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;
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: left;
margin-bottom: 15px;
position: relative;
}
label {
font-weight: bold;
display: block;
margin-bottom: 5px;
}
.password-container {
position: relative;
width: 100%;
}
input {
width: calc(100% - 40px);
padding: 10px;
padding-right: 35px; /* 預留空間給按鈕 */
margin-top: 5px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
}
.toggle-password {
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
border: none;
background: none;
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
}
.toggle-password img {
width: 20px;
height: 20px;
}
.button-group {
display: flex;
justify-content: space-between;
margin-top: 15px;
}
button {
width: 48%;
padding: 10px;
border: none;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
}
.save-button {
background: #28a745;
color: white;
}
.save-button:hover {
background: #218838;
}
.cancel-button {
background: #6c757d;
color: white;
}
.cancel-button:hover {
background: #5a6268;
}
.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">
<label for="old_password">舊密碼</label>
<div class="password-container">
<input type="password" id="old_password" name="old_password" required >
<button type="button" class="toggle-password" onclick="togglePassword('old_password', 'old-icon')">
<img src="eye-close.png" alt="顯示密碼" id="old-icon">
</button>
</div>
<label for="new_password">新密碼(至少8碼,含英文字母與數字):</label>
<div class="password-container">
<input type="password" id="new_password" name="new_password" required >
<button type="button" class="toggle-password" onclick="togglePassword('new_password', 'newpwd-icon')">
<img src="eye-close.png" alt="顯示密碼" id="newpwd-icon">
</button>
</div>
<label for="confirm_password">確認新密碼</label>
<div class="input-group">
<input type="password" id="confirm_password" name="confirm_password" required>
<button type="button" class="toggle-password" onclick="togglePassword('confirm_password', 'confirm-icon')">
<img src="eye-close.png" alt="顯示密碼" id="confirm-icon">
</button>
</div>
<div class="button-group">
<button type="submit" class="save-button">修改密碼</button>
<button type="button" class="cancel-button" onclick="window.location.href='member.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>
<script>
function togglePassword(inputId, iconId) {
let input = document.getElementById(inputId);
let icon = document.getElementById(iconId);
if (input.type === "password") {
input.type = "text";
icon.src = "eye-open.png";
} else {
input.type = "password";
icon.src = "eye-close.png";
}
}
// 初始載入時通知父頁更新 iframe 高度
setTimeout(() => {
if (typeof notifyParentOfHeightChange === "function") notifyParentOfHeightChange();
}, 300);
</script>
</body>
</html>