Home > Article > Backend Development > How to write a function to change the passwords of multiple users in PHP
PHP is a widely used server-side scripting language for developing dynamic websites and web applications. In web applications, it is usually necessary to manage multiple users, including user login, registration, information editing, etc. One common operation is to change the passwords of multiple users. This article will introduce how to use PHP to write a function that changes the passwords of multiple users.
1. Database design
Before writing, we need to design the database structure first. Assume that our system contains a user table named "users", which contains the following fields:
2. Create a password change form
Create a password change form in HTML. The form includes the following input boxes:
The following is a sample HTML code:
<form action="update_password.php" method="post"> <label for="old_password">旧密码:</label> <input type="password" name="old_password"> <br> <label for="new_password">新密码:</label> <input type="password" name="new_password"> <br> <label for="confirm_password">确认密码:</label> <input type="password" name="confirm_password"> <br> <button type="submit">修改密码</button> </form>
3. Process the password change request
Next, we have to write a PHP script to handle the password change request. First, get the data submitted by the form:
$old_password = $_POST['old_password']; $new_password = $_POST['new_password']; $confirm_password = $_POST['confirm_password'];
Then, verify the validity of the input data:
// 判断新密码和确认密码是否一致 if ($new_password != $confirm_password) { die("新密码和确认密码不一致。"); } // 验证旧密码是否正确 // 假设当前用户ID为1 $user = getUserById(1); if (!password_verify($old_password, $user['password'])) { die("旧密码不正确。"); }
Next, iterate through all users, change the password, and store the results in the array:
$results = array(); $users = getAllUsers(); foreach ($users as $user) { // 修改密码 $new_hashed_password = password_hash($new_password, PASSWORD_DEFAULT); $sql = "UPDATE users SET password = '$new_hashed_password' WHERE id = " . $user['id']; $result = mysqli_query($conn, $sql); if (!$result) { // 修改失败 $results[] = array( 'user_id' => $user['id'], 'username' => $user['username'], 'success' => false ); } else { // 修改成功 $results[] = array( 'user_id' => $user['id'], 'username' => $user['username'], 'success' => true ); } }
Finally, return the modification result to the user:
foreach ($results as $result) { if ($result['success']) { echo "用户 " . $result['username'] . " 的密码已成功修改。<br>"; } else { echo "用户 " . $result['username'] . " 的密码修改失败。<br>"; } }
4. Complete code
The following is the complete PHP code for modifying multi-user passwords:
<?php // 数据库连接 $conn = mysqli_connect('localhost', 'username', 'password', 'database'); // 获取当前用户信息 function getUserById($user_id) { global $conn; $sql = "SELECT * FROM users WHERE id = $user_id"; $result = mysqli_query($conn, $sql); $user = mysqli_fetch_assoc($result); return $user; } // 获取所有用户 function getAllUsers() { global $conn; $sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql); $users = mysqli_fetch_all($result, MYSQLI_ASSOC); return $users; } // 修改密码 function updatePassword($old_password, $new_password, $confirm_password) { global $conn; // 判断新密码和确认密码是否一致 if ($new_password != $confirm_password) { die("新密码和确认密码不一致。"); } // 验证旧密码是否正确 // 假设当前用户ID为1 $user = getUserById(1); if (!password_verify($old_password, $user['password'])) { die("旧密码不正确。"); } $results = array(); $users = getAllUsers(); foreach ($users as $user) { // 修改密码 $new_hashed_password = password_hash($new_password, PASSWORD_DEFAULT); $sql = "UPDATE users SET password = '$new_hashed_password' WHERE id = " . $user['id']; $result = mysqli_query($conn, $sql); if (!$result) { // 修改失败 $results[] = array( 'user_id' => $user['id'], 'username' => $user['username'], 'success' => false ); } else { // 修改成功 $results[] = array( 'user_id' => $user['id'], 'username' => $user['username'], 'success' => true ); } } foreach ($results as $result) { if ($result['success']) { echo "用户 " . $result['username'] . " 的密码已成功修改。<br>"; } else { echo "用户 " . $result['username'] . " 的密码修改失败。<br>"; } } } // 处理表单提交 if (isset($_POST['old_password']) && isset($_POST['new_password']) && isset($_POST['confirm_password'])) { updatePassword($_POST['old_password'], $_POST['new_password'], $_POST['confirm_password']); } ?>
5. Summary
PHP is a powerful server-side scripting language suitable for the development of various web applications. This article introduces how to use PHP to write a function that changes the passwords of multiple users. This function involves database connection, user authentication, data traversal and result output, etc. It has certain challenges for PHP beginners, but mastering the knowledge can provide a foundation for the next step of development.
The above is the detailed content of How to write a function to change the passwords of multiple users in PHP. For more information, please follow other related articles on the PHP Chinese website!