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

Jul 29, 2023 pm 05:09 PM
php development mall user management

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
);
Copy after login

[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();
}
?>
Copy after login

[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();
}
?>
Copy after login

[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();
}
?>
Copy after login

[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>
Copy after login

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles