Home >Backend Development >PHP Tutorial >How to use the PHP developer city function: build a user management system

How to use the PHP developer city function: build a user management system

WBOY
WBOYOriginal
2023-07-29 17:09:301627browse

How to use the PHP developer mall function: build a user management system

[Introduction]
In today's Internet era, e-commerce has become a very popular shopping method. More and more companies and Personal choice to conduct online business. The establishment of a complete user management system is an important part of mall development. This article will introduce how to use PHP to build a user management system to help developers quickly implement functions such as registration, login, password reset, and personal information management.

[Step 1: Create database and data table]
First, we need to create a database named "ecommerce" (can be changed according to the actual situation) in the MySQL database, and create a database named " users" data table. This data table will store user-related information, including username, password, email address, mobile phone number, etc.

CREATE DATABASE ecommerce;

USE ecommerce;

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,
  phone VARCHAR(20) NOT NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

[Step 2: Registration function implementation]
Next, we will write PHP code to implement the user registration function. It mainly includes verifying whether the user name, password, email and mobile phone number entered by the user are legal, and storing the information in the database.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $username = $_POST["username"];
  $password = $_POST["password"];
  $email = $_POST["email"];
  $phone = $_POST["phone"];

  // 输入验证...省略代码

  // 将用户信息插入数据库
  $conn = new mysqli("localhost", "username", "password", "ecommerce");
  $sql = "INSERT INTO users (username, password, email, phone) VALUES ('$username', '$password', '$email', '$phone')";

  if ($conn->query($sql) === TRUE) {
    echo "注册成功!";
  } else {
    echo "注册失败:" . $conn->error;
  }

  $conn->close();
}
?>

[Step 3: Login function implementation]
After the user registration is successful, we need to implement the login function. After the user enters the correct username and password, the program will verify whether the information is consistent with what is stored in the database. If it is consistent, the login is successful.

<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $username = $_POST["username"];
  $password = $_POST["password"];

  // 根据用户名从数据库获取密码
  $conn = new mysqli("localhost", "username", "password", "ecommerce");
  $sql = "SELECT * FROM users WHERE username='$username'";
  $result = $conn->query($sql);

  if ($result->num_rows == 1) {
    $row = $result->fetch_assoc();
    if (password_verify($password, $row["password"])) {
      $_SESSION["username"] = $username;
      header("Location: dashboard.php");
    } else {
      echo "密码错误!";
    }
  } else {
    echo "用户名不存在!";
  }

  $conn->close();
}
?>

[Step 4: Implementation of password reset function]
When the user forgets his password, we provide a password reset function. The user enters the email address filled in during registration, and the program will send an email containing a password reset link to the email address. After the user clicks the link, they can reset their password.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $email = $_POST["email"];

  // 根据邮箱从数据库获取用户名
  $conn = new mysqli("localhost", "username", "password", "ecommerce");
  $sql = "SELECT * FROM users WHERE email='$email'";
  $result = $conn->query($sql);

  if ($result->num_rows == 1) {
    $row = $result->fetch_assoc();
    // 发送密码重置邮件...省略代码
    echo "密码重置链接已发送至您的邮箱,请注意查收!";
  } else {
    echo "邮箱不存在!";
  }
  
  $conn->close();
}
?>

[Step 5: Personal information management function implementation]
After users log in, they can manage personal information. We can enable users to view and modify personal information.

<?php
session_start();

// 验证用户是否已登录
if (!isset($_SESSION["username"])) {
  header("Location: login.php");
  exit;
}

$username = $_SESSION["username"];

// 根据用户名从数据库获取用户信息
$conn = new mysqli("localhost", "username", "password", "ecommerce");
$sql = "SELECT * FROM users WHERE username='$username'";
$result = $conn->query($sql);
$user = $result->fetch_assoc();

$conn->close();
?>

<!-- 显示个人信息 -->
<h1>个人信息</h1>
<p>用户名:<?php echo $user["username"]; ?></p>
<p>邮箱:<?php echo $user["email"]; ?></p>
<p>手机号:<?php echo $user["phone"]; ?></p>

<!-- 修改个人信息 -->
<h2>修改个人信息</h2>
<form method="post" action="update_profile.php">
  <label for="email">邮箱:</label>
  <input type="email" name="email" value="<?php echo $user["email"]; ?>" required>
  <br>

  <label for="phone">手机号:</label>
  <input type="text" name="phone" value="<?php echo $user["phone"]; ?>" required>
  <br>

  <button type="submit">保存</button>
</form>

The above is a sample code for using the user management system in the PHP developer city function. Through the above steps, we can complete user registration, login, password reset, personal information management and other functions. Of course, this is only the basic function of user management. In actual development, further functional expansion and optimization are required according to specific needs.

[Summary]
This article introduces how to use the user management system in the PHP developer city function. We created databases and data tables and wrote codes to implement functions such as user registration, login, password reset, and personal information management. I hope this article can be helpful to PHP developers when building a user management system for a mall.

The above is the detailed content of How to use the PHP developer city function: build a user management system. 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