如何将PHP中随机生成的OTP /数字与用户输入进行比较

问题描述

我看过其他类似的线程,但找不到我想要的。

所以在这里,我为Otp生成一个随机数并将其发送给最终用户。现在我的问题是如何比较用户输入的值和PHP随机生成的数字。

我没有为此目的使用任何数据库,因此我需要免费的数据库解决方案。

这是我的HTML表单。

<html>
<head>
<h1> Working Otp sample</h1>
</head>
<body>
<form method="POST" action="sendotp.PHP">
<input type="tel" name="phone"> <br>
<button type="submit" name="sub">Submit</button>
</form>

<form method="POST" action="sendotp.PHP">
<input type="tel" name="otp"> <br>
<button type="submit" name="sub2">Submit</button>
</form>


</body>
</html>

这是发送和验证OTP的代码

我认为“ else if”中的if部分未执行,并且每次都调用$ myotp(否则else)会生成一个新数字。 我似乎找不到解决方案,请帮助。

在此先感谢您的时间和投入!

<?PHP
require __DIR__ . '/vendor/autoload.PHP';
use Twilio\Rest\Client;


if(isset ($_POST['sub']))
{
$phone = $_POST['phone'];

function generate()
{
    $digits_needed=6;

    $random_number=''; // set up a blank string

    $count=0;

    while ( $count < $digits_needed ) {
        $random_digit = mt_rand(0,9);
        
        $random_number .= $random_digit;
        $count++;
    }
    
    return $random_number;
}

$myotp = generate();

    
$account_sid = 'ACxxxxxxxxxxxxxxxxx';
$auth_token = 'xxxxxxxxxxxxxxxxxx';

$twilio_number = "+1234567890";

$client = new Client($account_sid,$auth_token);
$client->messages->create(
    // Where to send a text message (your cell phone?)
    $phone,array(
        'from' => $twilio_number,'body' => "Your OTP is $myotp"
    )
);
}
else if(isset ($_POST['sub2']))
{
    
    $otp = $_POST['otp'];
    echo "OTP is $otp";
    if($otp == $myotp)
    {
        echo "OTP successfully verified!";
    }
    else
    {
        echo "Please enter valid OTP";
    }
}


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)