Current Location: Home> Latest Articles> Complete Guide to Sending SMS Verification Code During PHP User Registration

Complete Guide to Sending SMS Verification Code During PHP User Registration

M66 2025-10-20

Introduction

With the rapid development of mobile internet, phone numbers have become an important credential for user registration and login. To enhance account security, many websites and applications send SMS verification codes during user registration. This article explains how to implement this feature in PHP and provides example code.

Creating the SMS Verification Code API

First, you need to create an API to send SMS codes to the user's phone. You can call a third-party SMS platform API or use your own service. Here is a sample interface:

<?php 
function send_sms_verification_code($phone, $code) {
// Call third-party SMS API to send the verification code to the user's phone
// Replace with actual API details based on your SMS platform
$api_url = 'http://smsapi.example.com/send_sms';
$api_key = 'YOUR_API_KEY';
$api_secret = 'YOUR_API_SECRET';

$params = [
    'phone' => $phone,
    'code' => $code,
    'api_key' => $api_key,
    'api_secret' => $api_secret,
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

return $response;
}
?>

In practice, implement the sending logic according to your SMS platform's API documentation.

Generating and Storing the Verification Code

During registration, generate a random verification code and store it in a database or cache for later validation. Example code:

<?php 
function generate_verification_code($length = 6) {
$characters = '0123456789';
$code = '';
for ($i = 0; $i < $length; $i++) {
    $code .= $characters[rand(0, strlen($characters) - 1)];
}
return $code;
}

function save_verification_code($phone, $code) {
// Store the phone number and code in the database or cache; this is just a sample
// Replace with your actual database implementation
$db = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
$stmt = $db->prepare('INSERT INTO verification_codes (phone, code) VALUES (:phone, :code)');
$stmt->bindParam(':phone', $phone);
$stmt->bindParam(':code', $code);
$stmt->execute();
}
?>

In this example, generate_verification_code() generates a random code, and save_verification_code() stores the phone number and code.

Calling the API to Send the Code

During registration, call the SMS sending API to deliver the verification code:

<?php
$phone = $_POST['phone']; // User submitted phone number
$code = generate_verification_code(); // Generate verification code
save_verification_code($phone, $code); // Store the code
$response = send_sms_verification_code($phone, $code); // Call the API

// Handle the response
if ($response == 'success') {
    echo 'SMS verification code has been sent, please check your phone';
} else {
    echo 'Failed to send SMS verification code, please try again later';
}
?>

Conclusion

Following these steps, you can implement SMS verification code sending during PHP user registration. Keep in mind that API implementations may vary by SMS platform, so adjust accordingly. The process includes generating the code, storing it, and sending it via an API to ensure user account security.