Current Location: Home> Latest Articles> Complete Guide to Implementing SMS Verification in PHP User Registration

Complete Guide to Implementing SMS Verification in PHP User Registration

M66 2025-07-17

Overview of Sending SMS Verification Code During PHP User Registration

In modern web applications, adding an SMS verification step during user registration is a common way to enhance security and ensure authenticity. This article explains how to implement SMS code verification in PHP with practical examples.

Choosing an SMS Service Provider

To enable SMS verification, you’ll need to integrate with a third-party SMS provider. Popular services include Alibaba Cloud, Tencent Cloud, and Yunpian. These platforms offer APIs that allow you to send SMS messages through simple HTTP requests.

Generating the Verification Code

When a user initiates registration, a random code should be generated and temporarily stored for later verification. Here’s a PHP function that generates a 6-digit numeric code:

function generateVerificationCode() {
    $code = '';
    for ($i = 0; $i < 6; $i++) {
        $code .= rand(0, 9);
    }
    return $code;
}

Sending the Verification Code via API

Once the code is generated, use your chosen provider’s API to send it to the user. The following PHP function demonstrates how to send an SMS using Yunpian's service:

function sendVerificationCode($phone, $code) {
    $apikey = 'your_api_key'; // Replace with your real API key
    $text = "【YourAppName】Your verification code is " . $code . ". It is valid for 5 minutes. Do not share it.";
    $url = 'https://sms.yunpian.com/v2/sms/single_send.json';

    $data = [
        'apikey' => $apikey,
        'mobile' => $phone,
        'text' => $text,
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}

Integrating the Registration Process

Once the functions are ready, you can integrate them into your user registration workflow. Here’s a sample function to handle user registration with SMS verification:

function registerUser($phone) {
    $verificationCode = generateVerificationCode();
    saveVerificationCodeToDatabase($phone, $verificationCode); // Store code in the database

    $sendResult = sendVerificationCode($phone, $verificationCode); // Send the code via SMS

    if ($sendResult == 'success') {
        echo 'Verification code sent successfully. Please check your SMS.';
    } else {
        echo 'Failed to send verification code. Please try again later.';
    }
}

Simply call registerUser($phone) after the user submits the registration form to complete the verification process.

Conclusion

With the provided code examples, implementing SMS verification during PHP user registration becomes straightforward. This process not only improves user experience but also enhances security. Always choose a reputable SMS provider to ensure reliability and consistent message delivery.