Table of Contents
用户注册
用户登录
Home Backend Development PHP Tutorial How to use PHP to develop user registration and login system

How to use PHP to develop user registration and login system

Aug 17, 2023 pm 01:27 PM
php log in system User registration

How to use PHP to develop user registration and login system

How to use PHP to develop user registration and login system

With the popularity and development of the Internet, user registration and login systems have become a must for almost all websites and applications. Must-have feature. This article will introduce how to use PHP to develop a simple user registration and login system, and provide code samples for reference.

1. Database design
Before developing the user registration and login system, we first design the database. Suppose we need to save the following information of the user: user name, password, email address, registration time, etc.

First, create a data table named users with the following structure:

CREATE TABLE `users` (
  `id` INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `username` VARCHAR(50) NOT NULL,
  `password` VARCHAR(255) NOT NULL,
  `email` VARCHAR(255) NOT NULL,
  `reg_time` DATETIME NOT NULL
);
Copy after login

2. Registration function
Next, we start to write the user registration function code.

First, create a file named register.php. In the file, we first verify whether the registration form data submitted by the user is legal, and then save the legal data to the database.

<?php
// 连接数据库
$conn = mysqli_connect('localhost', 'username', 'password', 'databasename');
if (!$conn) {
  die("数据库连接失败:" . mysqli_connect_error());
}

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $username = $_POST['username'];
  $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
  $email = $_POST['email'];
  $reg_time = date('Y-m-d H:i:s');

  // 检查用户名是否已存在
  $check_username = mysqli_query($conn, "SELECT * FROM users WHERE username='$username'");
  if (mysqli_num_rows($check_username) > 0) {
    die("用户名已存在");
  }

  // 保存用户数据到数据库
  $register_user = mysqli_query($conn, "INSERT INTO users (username, password, email, reg_time)
                          VALUES ('$username', '$password', '$email', '$reg_time')");

  if ($register_user) {
    echo "注册成功";
  } else {
    echo "注册失败:" . mysqli_error($conn);
  }
}

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

3. Login function
Next, we write the code for the user login function.

First, create a file named login.php. In the file, we verify whether the login form data submitted by the user is correct and process it accordingly based on the verification results.

<?php
// 连接数据库
$conn = mysqli_connect('localhost', 'username', 'password', 'databasename');
if (!$conn) {
  die("数据库连接失败:" . mysqli_connect_error());
}

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $username = $_POST['username'];
  $password = $_POST['password'];

  // 查询用户数据
  $query_user = mysqli_query($conn, "SELECT * FROM users WHERE username='$username'");
  if (mysqli_num_rows($query_user) > 0) {
    $user = mysqli_fetch_assoc($query_user);

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

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

4. Front-end page
Finally, we also need to create a front-end page to display the registration and login forms.

Registration page code example (register.html):

<!DOCTYPE html>
<html>
<head>
  <title>用户注册</title>
</head>
<body>
  <h1 id="用户注册">用户注册</h1>
  <form action="register.php" method="POST">
    <label>用户名:</label>
    <input type="text" name="username" required><br>
    <label>密码:</label>
    <input type="password" name="password" required><br>
    <label>邮箱:</label>
    <input type="email" name="email" required><br>
    <input type="submit" value="注册">
  </form>
</body>
</html>
Copy after login

Login page code example (login.html):

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

The above is the use of PHP to develop user registration and login system Simple example of . Through the above code, we can implement a basic user registration and login function. Of course, more functions and security measures need to be added in actual development, such as password encryption, verification codes, remembering login status, etc., to improve system security and user experience.

The above is the detailed content of How to use PHP to develop user registration and login 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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

CakePHP Creating Validators

DeepSeek official website entrance and latest promotional activities DeepSeek official website entrance and latest promotional activities Feb 19, 2025 pm 05:15 PM

DeepSeek official website entrance and latest promotional activities

See all articles