📲
Websites

Phone Number Sign-Up: Building SMS Verification Codes Properly

06.12.2026
← All articles

In Uzbekistan many people rarely use email, but nearly everyone has a phone number. Delivery services, online stores, gyms and training centres therefore often register users by phone and SMS code. For users it is simple; for developers the simple form hides several decisions about storage, code security and protecting the SMS budget from bots. Getting them right at the start is far cheaper than cleaning up duplicate accounts or an emptied SMS balance later.

The flow

  1. The user enters a number and the server normalises it.
  2. The server checks rate limits for that number and IP.
  3. A random code is generated; its hash and expiry are stored.
  4. The code is queued and sent by SMS.
  5. The user enters the code; the server verifies it and counts attempts.
  6. On success the account is created or logged in and the code is invalidated.

Normalise the number

Users type "+998 90 123-45-67", "901234567" or "998901234567". Store one 12-digit format starting with 998 and put a unique index on the column, or one person can open several accounts.

<?php
function normalize_uz_phone(string $input): ?string {
    $d = preg_replace('/\D+/', '', $input);
    if (strlen($d) === 9) { $d = '998' . $d; }
    return preg_match('/^998\d{9}$/', $d) ? $d : null;
}

Generate and store the code

Use random_int(), not rand(). Never store the plain code: keep an HMAC with a secret key and a short expiry, such as five minutes.

<?php
$code = (string)random_int(100000, 999999);
$hash = hash_hmac('sha256', $phone . '|' . $code, getenv('OTP_SECRET'));
$pdo->prepare("INSERT INTO phone_codes (phone, code_hash, expires_at, ip, created_at)
               VALUES (?, ?, NOW() + INTERVAL 5 MINUTE, ?, NOW())")
    ->execute([$phone, $hash, $_SERVER['REMOTE_ADDR']]);
queue_sms($phone, "Verification code: $code. Do not share it.");

Verify with an attempt limit

A six-digit code has a million combinations, which a bot can brute-force without limits. Allow around five attempts per code, compare with hash_equals() to avoid timing attacks, and delete the code once it succeeds so it cannot be reused. Look up only the newest unexpired code for the number, and count the attempt before comparing, so that a failed comparison can never skip the counter.

$pdo->prepare("UPDATE phone_codes SET attempts = attempts + 1 WHERE id=?")->execute([$c['id']]);
$try = hash_hmac('sha256', $phone . '|' . $input, getenv('OTP_SECRET'));
if ($c['attempts'] >= 5 || !hash_equals($c['code_hash'], $try)) { fail('Invalid code'); }

Protect your SMS balance

In "SMS pumping" attacks, bots submit thousands of numbers to trigger paid messages. Combine a 60-second resend cooldown and hourly cap per number, a per-IP limit across numbers, accepting only 998 numbers if your service is local, and an alert to the administrator when requests spike. A Tashkent gym site once saw a bot request codes for hundreds of numbers overnight; IP and hourly limits stopped most of the messages and the morning alert showed what had happened.

Usability

<input name="code" inputmode="numeric" autocomplete="one-time-code"
       pattern="\d{6}" maxlength="6" required>

These attributes open a numeric keypad and let the phone suggest the code from the SMS. Enable the resend button only after a visible countdown, and separate "wrong code", "code expired" and "too many attempts" in your error messages.

Number changes and personal data

Numbers change hands over time, so offer a backup contact such as email and require confirmation of both old and new numbers when changing them. Phone numbers are personal data under Uzbekistan's personal data legislation, including rules on where data is stored; confirm your obligations with a lawyer.

Related articles

⬜ WordPress White Screen of Death: Causes and a Step-by-Step Fix 💳 Connecting Click and Payme to a WooCommerce store 🧹 Cleaning the WordPress Database: Revisions and Transients ✅ Accepting a Website: What to Get from Your Developer
🌐 Language
🇺🇿 O⁠'zbek 🇺🇿 Özbek (yangi) 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓