Home >Backend Development >PHP Tutorial >PHP Implementation Guide for Mobile Number Verification Login Registration

PHP Implementation Guide for Mobile Number Verification Login Registration

WBOY
WBOYOriginal
2023-08-17 15:18:281246browse

PHP Implementation Guide for Mobile Number Verification Login Registration

PHP Implementation Guide for Mobile Number Verification Login and Registration

1. Overview
Mobile phone number verification is one of the common functions in modern Internet applications. It can not only It is used for user registration and login verification, and can also be used in scenarios such as sending SMS verification codes. This article will introduce how to use PHP language to implement the mobile phone number verification login registration function.

2. Environment requirements
Before starting to write code, we need to ensure that the following environment is ready:

  1. PHP environment: The version of PHP needs to be 5.6 or above.
  2. Database: This article uses the MySQL database as an example, so you need to ensure that MySQL has been installed and configured.

3. Database preparation
First, we need to prepare a database table for storing user information. We create a table named users, containing the following fields:

id INT PRIMARY KEY AUTO_INCREMENT,
mobile VARCHAR(11) UNIQUE,
password VARCHAR(32)

Among them, the mobile field is used to store the user's mobile phone number, and the password field is used to store the user's password. Please adjust according to actual needs.

4. Implementation of registration function

  1. Create the register.php file to process user registration requests.
  2. In register.php, first obtain the mobile phone number and password submitted by the user:

    $mobile = $_POST['mobile'];
    $password = $_POST['password'];
  3. Then, verify the validity of the mobile phone number. A common method to verify the validity of mobile phone numbers is to use regular expressions. The following is a simple example:

    if (!preg_match("/^1[34578]d{9}$/", $mobile)) {
     echo "手机号码不合法";
     exit;
    }
  4. Next, you need to determine whether the mobile phone number has been registered. You can query the database to determine whether the same mobile phone number already exists:

    // 连接数据库
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    if (!$conn) {
     die("数据库连接失败: " . mysqli_connect_error());
    }
    
    // 检查手机号码是否已存在
    $sql = "SELECT * FROM users WHERE mobile='$mobile'";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
     echo "手机号码已被注册";
     mysqli_close($conn);
     exit;
    }
    
    // 执行数据库插入操作
    $sql = "INSERT INTO users (mobile, password) VALUES ('$mobile', '$password')";
    if (mysqli_query($conn, $sql)) {
     echo "注册成功";
    } else {
     echo "注册失败";
    }
    
    // 关闭数据库连接
    mysqli_close($conn);

    5. Login function implementation

  5. Create the login.php file to process user login requests.
  6. In login.php, obtain the mobile phone number and password submitted by the user:

    $mobile = $_POST['mobile'];
    $password = $_POST['password'];
  7. Verify the validity of the mobile phone number:

    if (!preg_match("/^1[34578]d{9}$/", $mobile)) {
     echo "手机号码不合法";
     exit;
    }
  8. Connect to the database and query the user table to determine whether the mobile phone number and password match:

    // 连接数据库
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    if (!$conn) {
     die("数据库连接失败: " . mysqli_connect_error());
    }
    
    // 查询用户表
    $sql = "SELECT * FROM users WHERE mobile='$mobile' AND password='$password'";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
     echo "登录成功";
    } else {
     echo "手机号码或密码错误";
    }
    
    // 关闭数据库连接
    mysqli_close($conn);

    6. Summary
    This article introduces how to use PHP language to implement mobile phone number verification login Registration function. It should be noted that in actual development, more security verification and protection measures should be implemented for user input, such as verification code verification, password encrypted storage, etc. The code examples provided in this article can help developers quickly implement the mobile phone number verification login registration function, and can be further expanded and optimized according to actual needs.

The above is the detailed content of PHP Implementation Guide for Mobile Number Verification Login Registration. 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