Home >Backend Development >PHP Tutorial >Second-hand recycling website developed with PHP provides password retrieval function

Second-hand recycling website developed with PHP provides password retrieval function

王林
王林Original
2023-07-01 23:33:11712browse

The second-hand recycling website developed by PHP provides password retrieval function

With the rapid development of the Internet, second-hand recycling websites are becoming more and more popular. People can sell or recycle items they no longer need through these websites to achieve resource reuse and environmental protection. In the user management system of second-hand recycling websites, the password retrieval function is an essential function. This article will introduce how to use PHP to develop a second-hand recycling website and provide code examples for the password retrieval function.

In order to realize the password retrieval function, we first need to implement the user registration and login functions. This part of the code is as follows:

<?php
session_start();

// 连接数据库
$conn = new mysqli("localhost", "root", "password", "database_name");

if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 用户注册
if (isset($_POST['register'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    // 插入用户信息到数据库
    $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
    if ($conn->query($sql) === TRUE) {
        echo "注册成功";
    } else {
        echo "注册失败: " . $conn->error;
    }
}

// 用户登录
if (isset($_POST['login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    // 查询数据库,验证用户名和密码
    $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        $_SESSION['username'] = $username;
        echo "登录成功";
    } else {
        echo "用户名或密码错误";
    }
}
?>

Next, we need to implement the password retrieval function. This part of the code is as follows:

<?php
// 密码找回
if (isset($_POST['forgot_password'])) {
    $username = $_POST['username'];
    
    // 查询数据库,验证用户名
    $sql = "SELECT * FROM users WHERE username='$username'";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        // 生成一个随机的密码
        $new_password = generateRandomString(8);
        
        // 更新用户密码
        $sql = "UPDATE users SET password='$new_password' WHERE username='$username'";
        if ($conn->query($sql) === TRUE) {
            echo "新密码已发送至您的注册邮箱";
        } else {
            echo "密码重置失败: " . $conn->error;
        }
    } else {
        echo "用户名不存在";
    }
}

// 生成随机字符串
function generateRandomString($length) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    
    return $randomString;
}
?>

The above code implements the password retrieval function. When the user clicks the "Forgot Password" button, the system will send a new random password to the user's registered email address and store the password. The password is updated into the database. Users can log in again with this new password and change the password to their desired password after logging in.

When using the above code, you need to pay attention to the following points:

  1. You need to modify the database connection parameters and SQL statements according to the actual situation;
  2. Before sending For the function of adding a new password to the registered email address, you need to configure the SMTP server and the account and password for sending emails;
  3. In order to ensure security, it is recommended to use an encryption algorithm to encrypt and store the user password.

Summary: This article introduces how to use PHP to develop a second-hand recycling website, and provides code examples for the password retrieval function. Through this function, users can easily retrieve their passwords and improve the user experience and security of the website. Of course, in actual use, other functions can be added as needed, such as verification code verification, password strength check, etc. I hope this article will be helpful to develop the password retrieval function of second-hand recycling website in PHP.

The above is the detailed content of Second-hand recycling website developed with PHP provides password retrieval function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn