如何用php验证mysql的6位otp

时间:2016-08-27 19:20:43

标签: php

这是我的optverify.php代码,如果输入的数字是错误的,它重定向到index.php它没有给出错误。应该给错误的otp错误但是请帮我解决这个问题

  <?php
 // Create a unique instance of your session variables
session_start();
if(isset($_SESSION['usr_id']))
        {
            $uid=$_SESSION['usr_id'];
        } else {

            header("Location:login.php");
        }
require_once 'include/DB_Functions.php';
    $db = new DB_Functions();

if (isset($_POST['verifyotp'])) {
    $otpsms = $_POST['smsotp'];
    $otpemail = $_POST['emailotp'];

    $user = $db->verifyotp($uid);
    if($user){
        $user['smsotp'] = $otpsms;
        $user['emailotp'] = $otpemail;

    header("Location:index.php");

    } else {

        $errormsg = "Invalid otp";
    }

}

?>

我的数据库功能代码在

之下
public function verifyotp($uid){
        $stmt = $this->con->prepare("SELECT uid,smsotp,emailotp FROM users WHERE uid = '$uid'");
        $stmt->bind_param("i", $uid);
        if ($stmt->execute()) {

            $stmt->bind_result($uid,$smsotp,$emailotp);
            $stmt->fetch();
            $user = array();
            $user["uid"] = $uid;
            $user["smsotp"] = $smsotp;
            $user["emailotp"] = $emailotp;
            $stmt->close();
            return $user;
     } else 

     {
        return $stmt;
     }

   }

1 个答案:

答案 0 :(得分:0)

未经测试,但这应该工作!!如果这不起作用,请告诉我。将删除此答案。

<强>更新

将此$user = $db->verifyotp($uid);更改为$user = $db->verifyotp($uid, $otpsms, $otpemail);

如果你愿意测试3个参数(1)id(2)smsotp(3)emailotp,那么修改你的功能,如下所示。

public function verifyotp($uid, $sotp, $eotp){
        $stmt = $this->con->prepare("SELECT uid,smsotp,emailotp FROM users WHERE uid = '$uid' And smsotp='$sotp' And emailotp ='$eotp'");
        $stmt->bind_param("i", $uid);
        if ($stmt->execute()) {
            $stmt->bind_result($uid,$smsotp,$emailotp);
            $stmt->fetch();
            $user = array();
            $user["uid"] = $uid;
            $user["smsotp"] = $smsotp;
            $user["emailotp"] = $emailotp;
            $stmt->close();
            return $user;
     } else 

     {
        return $stmt;
     }

   }