Current Location: Home> Latest Articles> Detailed Guide to Implementing SMS Verification for User Registration and Login with PHP

Detailed Guide to Implementing SMS Verification for User Registration and Login with PHP

M66 2025-09-18

Implementing SMS Verification for User Registration and Login with PHP

With the rapid development of mobile internet, SMS verification has become an essential method for user registration and login. This article explains how to use PHP functions to implement SMS verification for registration and login, providing complete code examples for developers to reference.

Registration Function

During user registration, users need to enter their mobile number and request a verification code. The backend calls a third-party SMS API to send the code to the user's phone. Users then enter the code to complete the registration process.

Registration Code Example

<?php
// Generate random verification code
function generateCode($length = 4)
{
    $chars = '0123456789';
    $code = '';
    for ($i = 0; $i < $length; $i++) {
        $code .= $chars[random_int(0, strlen($chars) - 1)];
    }
    return $code;
}

// Send verification code
function sendCode($mobile, $code)
{
    // Call SMS API to send the code
    // Code omitted
}

// Registration handler
function register()
{
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $mobile = $_POST['mobile'];
        $code = generateCode();
        sendCode($mobile, $code);
        // Store the code in session for later verification
        session_start();
        $_SESSION['code'] = $code;
        echo 'Verification code sent! Please check your phone.';
    }
}
?>

In the code above, generateCode generates a random verification code, sendCode sends it via SMS, and register handles the registration process by generating, sending, and storing the code in the session.

Login Function

During login, users need to input their mobile number and verification code. The backend sends the code through an SMS API, and users enter it to verify and complete the login process.

Login Code Example

<?php
// Verify code
function verifyCode($code)
{
    session_start();
    if (isset($_SESSION['code']) && $_SESSION['code'] == $code) {
        return true;
    } else {
        return false;
    }
}

// Login handler
function login()
{
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $mobile = $_POST['mobile'];
        $code = $_POST['code'];
        if (verifyCode($code)) {
            // Verification passed, perform login
            echo 'Login successful!';
        } else {
            echo 'Incorrect verification code!';
        }
    }
}
?>

In the above code, verifyCode checks whether the submitted code matches the one stored in the session, and login handles the login process, including verification and login execution.

Conclusion

This article explained how to implement SMS verification for user registration and login using PHP functions, including complete code examples. Developers can modify and extend the code according to their project requirements to quickly build a secure and reliable registration and login verification system.