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:
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'] . " 的密码已成功修改。
"; } else { echo "用户 " . $result['username'] . " 的密码修改失败。
"; } }
4. Complete code
The following is the complete PHP code for modifying multi-user passwords:
$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'] . " 的密码已成功修改。
"; } else { echo "用户 " . $result['username'] . " 的密码修改失败。
"; } } } // 处理表单提交 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!