Mobile Number Verification via OTP SMS using PHP
This tutorial will help you:
-
Generate an OTP
-
Send it to a mobile number via SMS
-
Verify the entered OTP on your website
Requirements
-
PHP server (XAMPP/LAMP/WAMP or Live Server)
-
MySQL (for storing OTPs, optional)
Choose an SMS Gateway API
You can use any SMS service provider. Popular options:
In this example, we'll use Fast2SMS (free tier in India)
You’ll need to:
-
Create an account
-
Get the API key from your dashboard
Step-by-Step Implementation
1. HTML Form to Enter Mobile Number
2. PHP Script to Send OTP (send_otp.php
)
3. OTP Verification Form (verify_otp.php
)
4. Check OTthe P (check_otp.php
)
Optional: Store OTP in Database (More secure)
Instead of using $_SESSION,
You can store:
-
mobile
-
otp
-
timestamp
Then, verify within a time limit (e.g., 5 minutes).
Security Tips
-
Expire OTPs after 5-10 minutes.
-
Rate limit OTP requests per number/IP.
-
Never expose your API key in frontend code.