Home  >  Article  >  Backend Development  >  How to use PHP to develop user registration and login system

How to use PHP to develop user registration and login system

王林
王林Original
2023-08-17 13:27:231191browse

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

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);
?>

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);
?>

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>用户注册</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>

Login page code example (login.html):

<!DOCTYPE html>
<html>
<head>
  <title>用户登录</title>
</head>
<body>
  <h1>用户登录</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>

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