
The short answer (for developers): a code sent by SMS is only as safe as the code around it. The rules: generate it with a secure random generator, store only a hash of it, with a short validity, an attempt limit and single use; limit how many SMS each number and each IP can trigger; and never write the code into logs, URLs or analytics tools. Remember too that the SMS itself can be read on a locked screen or diverted through a SIM swap: for accounts that matter, also offer an authenticator app (authenticator app, SMS or security key).
The risks and the defences
| Risk | Defence |
| Guessing the code by brute force | An attempt limit per code; after it, the code stops working. Compare with hash_equals. |
| A code that still works hours later | Short validity, single use, and a new code cancels the previous one. |
| Your form used to fire off SMS in bulk (inflated traffic to numbers that pay whoever runs the scam) | Limits per number, per IP and per session; a captcha before sending; only the countries you serve; an alarm when volume is out of the ordinary. |
| The code shows up in logs or monitoring | Log “code sent to the number ending in…”, never the code. Check the HTTP client’s debug mode and any tool that stores request bodies. |
| A code requested for one thing used for another | Tie the code to the user, the purpose (log in, change password, confirm a payment) and the session. |
| A predictable code | random_int, which uses the system’s secure generator. Never rand(), mt_rand() or anything derived from the time. |
How to do it, step by step (PHP)
|
|
|
|
|
|
| Never return the code to the browser “to make testing easier”, and never leave a fixed test code active in production. Both are classic mistakes, and either one cancels the whole check. |
| Many SMS providers have their own verification API, which generates, sends and checks the code and handles part of the limits. Using it moves part of this work to them; read what they keep and for how long. And do not wait for the delivery report before letting the user carry on: delivery reports (DLR). |
|
Want to limit requests to the code form at server level, on your VPS? Open a ticket and tell us the form’s address. Open a support ticket |
RECOMMENDED PRODUCT Web hosting with cPanel Domain and SSL included, daily backups and the panel you already know. from $6.60/mo (3-year plan, with coupon) See plans |











