Press "Enter" to skip to content

php获取手机验证码常用逻辑

Last updated on 2019-05-15

<?php
$con    = mysqli_connect('localhost', 'root', '', 'test');
$phone      = $_POST['phone'];
$time       = time();
$start_time = strtotime(date('Y-m-d H:00:00'));
$end_time   = strtotime(date('Y-m-d H:59:59')); //查询当前1个小时内获取验证码的次数
$sql1       = "select count(*) as id from phone where phone = {$phone} and `time` >= {$start_time} and `time` <= {$end_time}";
$res1       = mysqli_query($con, $sql1);
$row1[]     = mysqli_fetch_assoc($res1);
if ($row1[0]['id'] > 4) {
    echo "一个小时内验证码只能获取五次";
} else {
    $sql2 = "select max(time) as time from phone where phone = $phone";
    $res2 = mysqli_query($con, $sql2);
    $row2 = mysqli_fetch_assoc($res2);
//如果最大的时间与当前时间小于60s
    $stime = $time - $row2['time'];
    if ($stime <= 60) {
        echo "两次验证码获取间隔之间小于60s";
    } //否则发送验证码并插入当前最新获取验证码的时间
    else {
        echo "你的验证码是xxx";
        $data   = "insert into phone(phone,time) values($phone,$time)";
        $insert = mysqli_query($con, $data);
    }

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *