📘 Lesson  ·  Lesson 46

OTP Verification

OTP Verification

About

OTP (One-Time Password) verification adds security — a random code is sent to the user and checked before allowing access.

Generate and Store OTP

session_start();
$otp = rand(100000, 999999);       // 6-digit OTP
$_SESSION["otp"] = $otp;
$_SESSION["otp_time"] = time();
// then email/SMS the $otp to the user
mail($userEmail, "Your OTP", "Your code is: $otp");

Verify OTP

$entered = $_POST["otp"];
if ($entered == $_SESSION["otp"] && (time() - $_SESSION["otp_time"]) < 300) {
    echo "OTP verified!";   // valid for 5 minutes
} else {
    echo "Invalid or expired OTP";
}

Summary

  • Generate a random OTP, store it in session, and send to the user.
  • Verify the entered code and check it has not expired (e.g. 5 minutes).

परिचय

OTP (One-Time Password) verification security बढ़ाता है — user को random code भेजा जाता है और access देने से पहले जाँचा जाता है।

OTP Generate और Store करें

session_start();
$otp = rand(100000, 999999);       // 6-digit OTP
$_SESSION["otp"] = $otp;
$_SESSION["otp_time"] = time();
// फिर $otp को user को email/SMS करें
mail($userEmail, "Your OTP", "Your code is: $otp");

OTP Verify करें

$entered = $_POST["otp"];
if ($entered == $_SESSION["otp"] && (time() - $_SESSION["otp_time"]) < 300) {
    echo "OTP verified!";   // 5 मिनट valid
} else {
    echo "Invalid or expired OTP";
}

सारांश

  • Random OTP generate करें, session में store करें, user को भेजें।
  • Entered code verify करें और expire न हुआ हो जाँचें (जैसे 5 मिनट)।
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।