Home > front end >  Send and Verify OTP without Saving the Data in the database?
Send and Verify OTP without Saving the Data in the database?

Time:12-19

I am trying to implement OTP Verification in a Signup form Via PHP the thing is that I don't want to save the user's data unless they have verified their Email for the sake of saving the Database's Bandwidth but the Problem is that if I will not save the user's Data in the database how will I be able to verify their Email?

Any thoughts on that?

Thanks

CodePudding user response:

Here's a rough example, you will need to edit it to fit your project obviously. Maybe you're submitting to a different script, ask for email from a different script, etc.

You can leverage the session to store data temporarily.

session_start();



echo '<form action="" method="post">
    <input type="text" name="otp" />
    <input type="submit" value="Submit" />
</form>';

// if the form is submitted, then check for OTP, otherwise generate it and store it in session
if (isset($_POST['otp'])) {
    // check if the OTP is correct
    if ($_POST['otp'] == $_SESSION['otp']) {
        //do something
    } else {
        // do something else
    }
}
else{
    $otp = rand(100000, 999999); //this is not a safe way to generate an OTP, but just for examples sake
    $_SESSION['otp'] = $otp;
    // code that sends the OTP to the user here using mail, text or API... 
    // ...
}
  •  Tags:  
  • php
  • Related