【发布时间】:2018-04-12 06:54:51
【问题描述】:
我想创建一个 OTP 发送器和检查器。
代码是这样的:
<?php
$Mail = "someone@gmail.com";
$OTP = mt_rand();
$subject = "OTP from HelloBuddy.co.in";
$txt = "You have recieved OTP from HelloBuddy.co.in. The OTP is $OTP. Please enter the OTP in the website. Please note that the OTP will expire in 100 seconds.";
if (mail($Mail,$subject,$txt)){
echo "<p>OTP has been successfully sent<br>The OTP will expire in <span id='xpire'>100</span> seconds.<br>Please enter the OTP here:<input type='number' name ='OTP'></p>";
echo "<script>
var a = 99;
var x = setInterval(cdown, 1000);
function cdown(){
if (a>=0) {
document.getElementById('xpire').innerHTML = a;
a=a-1;
} else {
clearInterval(x);
clearOTP()
}
function clearOTP{
$OTP = '';
}
</script>";
}else{
die(ERROR);
}
?>
我正在使用 xampp 服务器并已正确配置所需文件。
我想在 100 秒后清除 OTP,但无法清除,可能是因为它位于 javscript 函数中。
如果可能,请提供 AJAX。
【问题讨论】:
-
您可以在 javascirpt 中使用 php 变量,方法如下:“”。
-
但我的 javscript 代码也在 中。在另一个
??? 里面可以有 -
@HyperMaths.com 最好的方法是在会话中存储 OTP。
-
我想指出,将 html/js/php/css 混合在一起是一种不好的做法,会使代码难以阅读、修改并为难以发现的缺陷创造空间。请查看关注点分离、MVC 框架和模板。
-
谢谢你。我已经正确格式化了。
标签: javascript php ajax one-time-password