Home  >  Article  >  Backend Development  >  Discussion on the mall user login and registration function developed by PHP

Discussion on the mall user login and registration function developed by PHP

WBOY
WBOYOriginal
2023-07-02 12:27:07814browse

Discussion on the mall user login and registration function developed by PHP

With the vigorous development of e-commerce, mall websites have become the first choice for people to shop. When it comes to realizing the user login and registration function of the mall website, PHP, as a server-side scripting language that is powerful, easy to learn and use, has become the first choice of many developers.

This article will discuss how to use the PHP Developer City user login and registration function, and attach corresponding code examples.

First, we need to create a database to store the user's login registration information. Suppose we already have a database named "shop" and create a table named "users" in the database. The table contains the following fields: id (auto-increment primary key), username (user name), password (password) ).

Next, we need to create a user registration page (register.php). The page provides input boxes for username, password, and password confirmation, and includes a submit button. After the user enters the username and password and clicks the submit button, we will use PHP to store the user registration information into the database.

The following is a code example of register.php:

<!DOCTYPE html>
<html>
<head>
    <title>用户注册</title>
</head>
<body>
    <h2>用户注册</h2>
    <form action="register.php" method="POST">
        <label for="username">用户名:</label>
        <input type="text" name="username" required><br><br>
        
        <label for="password">密码:</label>
        <input type="password" name="password" required><br><br>
        
        <label for="confirm_password">确认密码:</label>
        <input type="password" name="confirm_password" required><br><br>
        
        <input type="submit" name="submit" value="注册">
    </form>

    <?php
    if(isset($_POST['submit'])){
        $username = $_POST['username'];
        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];
        
        if($password == $confirm_password){
            $conn = mysqli_connect("localhost", "root", "password", "shop");
            
            $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
            mysqli_query($conn, $sql);
            
            echo "注册成功!";
        } else {
            echo "两次密码输入不一致,请重新输入!";
        }
    }
    ?>
</body>
</html>

When the user enters the username and password on the register.php page and clicks the submit button, the PHP code will obtain the username entered by the user. Password and confirmation password, and compare it with the data already in the database. If the two password inputs are consistent, the PHP code will insert the user registration information into the database table, and the registration is successful.

After the registration is completed, we also need to create a user login page (login.php). This page provides input boxes for username and password, and also contains a submit button. When the user clicks the submit button, we will use PHP to verify that the username and password entered by the user are correct.

The following is a code example of login.php:

<!DOCTYPE html>
<html>
<head>
    <title>用户登录</title>
</head>
<body>
    <h2>用户登录</h2>
    <form action="login.php" method="POST">
        <label for="username">用户名:</label>
        <input type="text" name="username" required><br><br>
        
        <label for="password">密码:</label>
        <input type="password" name="password" required><br><br>
        
        <input type="submit" name="submit" value="登录">
    </form>

    <?php
    if(isset($_POST['submit'])){
        $username = $_POST['username'];
        $password = $_POST['password'];
        
        $conn = mysqli_connect("localhost", "root", "password", "shop");
            
        $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
        $result = mysqli_query($conn, $sql);
        
        if(mysqli_num_rows($result) > 0){
            echo "登录成功!";
        } else {
            echo "用户名或密码错误,请重新输入!";
        }
    }
    ?>
</body>
</html>

When the user enters the username and password on the login.php page and clicks the submit button, the PHP code will obtain the username and password entered by the user. , and verify whether the user's login information is correct by comparing it with the data in the database. If the username and password are consistent, the PHP code will output "Login successful", otherwise it will output "Username or password is incorrect, please re-enter".

Through the above code example, we can implement the login and registration function for mall users. Of course, in actual development, more security and data verification are needed. I hope this article will be helpful to you when developing the PHP mall user login and registration function.

The above is the detailed content of Discussion on the mall user login and registration function developed by PHP. 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