Php OTP在登记表上的整合
我在注册表格中集成“发送OTP”功能时遇到问题。我得到了一个来自SMS提供商的API,但我不知道如何将其集成到我的表单中。我需要在验证OTP后将用户数据记录在我的数据库中。但是验证过程是如何工作的呢?系统如何为用户生成6位随机码?我一直在尝试一种不同的方法,并在网上搜索,但这些都不起作用。有人能帮忙吗 这是我的表格:Php OTP在登记表上的整合,php,html,one-time-password,Php,Html,One Time Password,我在注册表格中集成“发送OTP”功能时遇到问题。我得到了一个来自SMS提供商的API,但我不知道如何将其集成到我的表单中。我需要在验证OTP后将用户数据记录在我的数据库中。但是验证过程是如何工作的呢?系统如何为用户生成6位随机码?我一直在尝试一种不同的方法,并在网上搜索,但这些都不起作用。有人能帮忙吗 这是我的表格: <div class="modal-body"> <form action="includes/signu
<div class="modal-body">
<form action="includes/signup.inc.php" method="POST" class="p-3">
<div class="form-group">
<label for="recipient-name" class="col-form-label">First Name</label>
<input type="text" class="form-control" placeholder="First Name" name="first" required="">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Last Name</label>
<input type="text" class="form-control" placeholder="Last Name" name="last" required="">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Username</label>
<input type="text" class="form-control" placeholder="Username" name="uid" required="" >
</div>
<div class="form-group">
<label for="recipient-name1" class="col-form-label">Date of Birth</label>
<input type="date" class="form-control" placeholder="dob" name="dob" required="">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Email Address</label>
<input type="email" class="form-control" placeholder="Email" name="email" required="" >
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Password</label>
<input type="password" class="form-control" placeholder="Password" name="pass" required="">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Confirm Password</label>
<input type="password" class="form-control" placeholder="Confirm Password" name="c_pass" required="">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Are You Previously an Existing Member?</label>
<select class="form-control" id="recipient-name10" name="member">
<option>Yes</option>
<option>No</option>
</select>
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Where do you know about this membership?</label>
<select class="form-control" id="recipient-name11" name="outlet">
<option>The Metallic Kitchen @ Golden Triangle Pelangi, JB</option>
<option>The Metallic Kitchen @ Taman Mount Austin, JB</option>
<option>The Metallic Kitchen & Bar @ Setapak Village, KL</option>
<option>None of the above</option>
</select>
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">OTP</label>
<input type="text" class="form-control" placeholder="OTP" name="otp" required="">
</div>
<div class="right-w3l mt-4 mb-3">
<input type="submit" class="form-control" value="Create account" name="submit">
</div>
</form>
</div>
名字
姓
用户名
出生日期
电子邮件地址
密码
确认密码
您以前是现有会员吗?
对
不
你从哪里知道这个会员资格?
金三角佩兰吉金属厨房,JB
JB奥斯汀塔曼山的金属厨房
吉隆坡Setapak村的金属厨房和酒吧
以上都没有
检察官办公室
这是我的短信提供商API:
<?php
function sendSmsToEsms() {
$url = 'https://api.esms.com.my/sms/send';
// replace yourusername, yourpassword, and 60123456789 to suits your need
$data = array('user' => 'yourusername',
'pass' => 'yourpassword',
'to' => '60123456789',
'msg' => 'RM0.00 Hello from ESMS');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded; charset=utf-8",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
}
?>
以下是我向数据库中添加数据的代码:
<?php
if (isset($_POST['submit'])){
include_once 'db.php';
$first = mysqli_real_escape_string($conn,$_POST['first']);
$last = mysqli_real_escape_string($conn,$_POST['last']);
$uid = mysqli_real_escape_string($conn,$_POST['uid']);
$dob = mysqli_real_escape_string($conn,$_POST['dob']);
$email = mysqli_real_escape_string($conn,$_POST['email']);
$mobile = mysqli_real_escape_string($conn, $_POST['m_number']);
$pwd = mysqli_real_escape_string($conn,$_POST['pass']);
$member =mysqli_real_escape_string($conn, $_POST['member']);
$outlet = mysqli_real_escape_string($conn,$_POST['outlet']);
//ERROR HANDLERS
//CHECK FOR EMPTY FIELDS
//if(empty($first)||empty($last)||empty($uid)||empty($dob)||empty($email)||empty($mobile)||empty($pwd)||empty($member)||empty($outlet))
//{
//header("Location:../index.php?signup=empty");
//exit();
//}else{
//check if input characters are valid
//if(!preg_match("/^[a-zA-Z]*$/", $first)|| !preg_match("/^[a-zA-Z]*$/", $last)){
//header("Location:../signup.php?signup=invalid");
//exit();
//}else{
//check email
if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
echo "<script>alert('Invalid Email,please register again.')</script>";
echo "<script>window.open('../index.php','_self')</script>";
exit();
}else{
//check if username is same
$sql = "SELECT * FROM users WHERE user_uid = '$uid'";
$result = mysqli_query ($conn,$sql);
$resultCheck = mysqli_num_rows ($result);
if ($resultCheck > 0) {
echo "<script>alert('Username has been taken, please register again.')</script>";
echo "<script>window.open('../index.php','_self')</script>";
exit();
}else{
//Hashing pwd
$hashedPwd = password_hash($pwd,PASSWORD_DEFAULT);
//INSERT THE USER INTO THE DATABASE
$sql = "INSERT INTO users (user_first,user_last,user_uid,user_dob,user_email,user_mobile,user_pwd,user_member,user_outlet) VALUES ('$first','$last','$uid','$dob','$email','$mobile','$hashedPwd','$member','$outlet');";
mysqli_query($conn,$sql);
echo "<script>alert('You have been Registered Successfully, Please login from our main page')</script>";
echo "<script>window.open('../index.php','_self')</script>";
exit();
}
}
}
else{
header("Location:../index.php");
exit();
}
?>
您应该将用户数据临时保存到数据库中,同时保存生成的OTP和一个额外的列,以指示用户是否已验证。(我建议在保存之前先散列OTP)
稍后,当用户尝试使用用户名和OTP登录时,您应该对照数据库检查输入的数据。如果用户和OTP正确,请检查该列以验证注册。如果OTP不正确,您可以保留该列以进行更多尝试(或删除用户帐户或作废OTP或根据您的意见重新生成新的OTP)
要生成随机数,请使用mt_rand算法:
$password=mt_rand (10,100);
并在API中使用它,如下所示:
'pass' => $password,
检查表单发布的代码在哪里?正如我所看到的,您必须替换用户并从提供商处传递给您的用户,to是客户端号码,msg是您希望接收的消息。你说的是什么6位数字?我已经更新了代码@Delboy1978UK我说的6位数字是当输入他们的电话号码时,系统会生成一个随机的6位数字代码并发送到用户的电话号码,以验证他们的电话号码是否正确,但是我被困在如何生成它和如何验证它上。@D.dimitrovb但我试图实现的是:第一:用户在我的表单上注册第二:在电话号码框旁边的同一表单上有一个发送otp按钮,当它单击时,otp将发送给用户第三个电话:otp中的用户密钥并单击提交,所有数据都将记录在我的数据库中。您不应将otp会话设置为基于,因为用户可能由于多种原因而失去会话。标准方法是将第一个表单临时保存,并等待OTP检查以决定删除或确认这些临时数据@louis