PHP实现TXT文件中指定用户的密码修改功能
修改TXT存储中指定用户的密码实现方案
这是作业需求,不能使用SQL。现有程序通过注册表单生成users.txt存储用户数据,格式为用户名|手机号|密码,每行一个用户,注册功能正常。现在要实现修改密码表单,已经能匹配用户名和手机号,但不知道如何修改TXT文件中对应用户的密码,现有代码如下:
现有HTML代码
<form method="POST" action="change.php"> <div class="form-group"> <label for="username">Username:</label> <input type="text" class="form-control" placeholder="Enter username" name="username" /> </div> <div class="form-group"> <label for="telephone">Telephone:</label> <input type="text" class="form-control" placeholder="Enter telephone" name="phone" /> </div> <button type="submit" class="btn btn-secondary">Register</button> </form>
现有change.php代码
$username = $_POST['username']; $phone = $_POST["phone"]; $users = file_get_contents("users.txt"); $users = trim($users); $users = explode(PHP_EOL, $users); foreach ($users as $user) { $user = explode("|", $user); $password = $user[2]; // This way i managed to select the $password $password = "newpassword"; var_dump($password); //When i click submit it shows the new password but i don't know how to change just the $password in the .txt /* if ($username == $user[0] && $phone == $user[1]) { header("Location: index.php?status=userFound"); die(); } else { header("Location: index.php?status=userNotFound"); die(); } */
解决方案
要完成密码修改,需要补充表单输入、处理用户数据并写回文件,具体修改如下:
1. 完善修改密码表单
添加新密码输入框,修正按钮文字:
<form method="POST" action="change.php"> <div class="form-group"> <label for="username">Username:</label> <input type="text" class="form-control" placeholder="Enter username" name="username" required /> </div> <div class="form-group"> <label for="telephone">Telephone:</label> <input type="text" class="form-control" placeholder="Enter telephone" name="phone" required /> </div> <div class="form-group"> <label for="newpassword">New Password:</label> <input type="password" class="form-control" placeholder="Enter new password" name="newpassword" required /> </div> <button type="submit" class="btn btn-secondary">修改密码</button> </form>
- 新增
newpassword输入框,用type="password"隐藏输入内容 - 添加
required属性确保必填项不为空 - 修正按钮文字为“修改密码”
2. 修改change.php逻辑
实现用户匹配、密码修改和文件写入:
<?php // 检查必填参数是否齐全 if (!isset($_POST['username'], $_POST['phone'], $_POST['newpassword'])) { header("Location: index.php?status=missingParams"); die(); } $username = trim($_POST['username']); $phone = trim($_POST['phone']); $newPassword = trim($_POST['newpassword']); // 读取用户文件,自动跳过空行和处理换行 $users = file("users.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $userFound = false; $updatedUsers = []; foreach ($users as $userLine) { $userData = explode("|", $userLine); // 跳过格式错误的行 if (count($userData) !== 3) { $updatedUsers[] = $userLine; continue; } // 匹配目标用户 if ($userData[0] === $username && $userData[1] === $phone) { // 修改密码并重新组装行 $userData[2] = $newPassword; $updatedUsers[] = implode("|", $userData); $userFound = true; } else { // 非目标用户直接保留原数据 $updatedUsers[] = $userLine; } } // 将更新后的所有用户数据写入文件 file_put_contents("users.txt", implode(PHP_EOL, $updatedUsers) . PHP_EOL); // 根据结果跳转对应页面 if ($userFound) { header("Location: index.php?status=passwordChanged"); } else { header("Location: index.php?status=userNotFound"); } die(); ?>
- 先校验参数完整性,避免无意义的处理
- 使用
file()函数简化文件读取,自动处理换行和空行 - 遍历中仅修改匹配用户的密码,其余用户数据保持不变
- 所有处理完成后统一写入文件,避免中途跳转导致数据丢失
- 最后根据用户是否找到的结果跳转对应状态页面
内容的提问来源于stack exchange,提问作者Raitiko
相关产品推荐
相关产品推荐

