Home > Backend Development > PHP Tutorial > How to use PHP developer city function: build user registration and login system

How to use PHP developer city function: build user registration and login system

WBOY
Release: 2023-07-29 08:34:02
Original
1341 people have browsed it

How to use the PHP developer mall function: Build a user registration and login system

With the rapid development of e-commerce, building a mall website has become the top priority for many companies and individual entrepreneurs. When building a mall website, the user registration and login system is one of the most basic and important functions. This article will introduce how to use PHP language to implement the mall website user registration and login system, and attach code examples to help readers quickly understand the specific implementation steps.

  1. Database design

First, we need to design the database to store user-related information. Taking MySQL as an example, we can create a table named "users" with the following table structure:

CREATE TABLE users (
  id INT(11) AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(50) NOT NULL,
  password VARCHAR(255) NOT NULL,
  email VARCHAR(100) NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;
Copy after login
  1. User registration page

The user registration page is where users register For the entrance, we can create a file named "register.php" with the following code:

<!DOCTYPE html>
<html>
<head>
  <title>用户注册</title>
</head>
<body>
  <h3>用户注册</h3>
  <form method="POST" action="register_process.php">
    <label for="username">用户名:</label>
    <input type="text" name="username" required><br>

    <label for="password">密码:</label>
    <input type="password" name="password" required><br>

    <label for="email">邮箱:</label>
    <input type="email" name="email" required><br>

    <button type="submit">注册</button>
  </form>
</body>
</html>
Copy after login
  1. User registration processing

After the user enters the information on the registration page , which needs to be submitted to the server for processing. We can create a file named "register_process.php" with the following code:

<?php
// 连接数据库
$conn = mysqli_connect("localhost", "root", "password", "database_name");

// 获取用户提交的信息
$username = $_POST['username'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$email = $_POST['email'];

// 查询用户名是否已存在
$query = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
  echo "该用户名已被使用,请重新选择";
  exit();
}

// 插入用户信息到数据库
$query = "INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')";
if (mysqli_query($conn, $query)) {
  echo "用户注册成功";
} else {
  echo "用户注册失败";
}

// 关闭数据库连接
mysqli_close($conn);
?>
Copy after login
  1. User login page

The user login page is the entrance for users to log in. We You can create a file named "login.php" with the following code:

<!DOCTYPE html>
<html>
<head>
  <title>用户登录</title>
</head>
<body>
  <h3>用户登录</h3>
  <form method="POST" action="login_process.php">
    <label for="username">用户名:</label>
    <input type="text" name="username" required><br>

    <label for="password">密码:</label>
    <input type="password" name="password" required><br>

    <button type="submit">登录</button>
  </form>
</body>
</html>
Copy after login
  1. User login processing

After the user enters the information on the login page, it needs to be Submit to server for verification. We can create a file named "login_process.php" with the following code:

<?php
// 连接数据库
$conn = mysqli_connect("localhost", "root", "password", "database_name");

// 获取用户提交的信息
$username = $_POST['username'];
$password = $_POST['password'];

// 查询用户信息
$query = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);

// 验证密码
if (password_verify($password, $row['password'])) {
  echo "登录成功";
} else {
  echo "用户名或密码错误";
}

// 关闭数据库连接
mysqli_close($conn);
?>
Copy after login

Through the above steps, we have successfully built a simple user registration and login system. When the user fills in the registration information, the data will be saved in the database; when the user fills in the login information, the system will compare it with the information in the database for verification. Readers can further optimize and expand the system according to their own needs, adding more functions and security verification.

Summary

User registration and login system is one of the core functions of the mall website. In order to realize user registration and login, we can use PHP language and database for development. This article helps readers understand and master the implementation principles and specific steps of the user registration and login system by introducing the design of the database and the creation of user registration and login pages, as well as corresponding processing code examples. I hope that readers can make better use of the PHP developer city functions through this article and achieve a more complete and secure user registration and login system.

The above is the detailed content of How to use PHP developer city function: build user registration and login system. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template