【问题标题】:Checking for valid OTP using session variable使用会话变量检查有效的 OTP
【发布时间】:2015-10-25 01:55:34
【问题描述】:

我正在尝试发送 SMS OTP 并从用户那里检索它以验证用户。 我的 SMS 脚本工作正常,但我无法存储会话变量并检查该特定用户的 OTP 是否有效。

index.php

<?php
$otp1 = '';
for($i=4;$i>0;$i--)
{
    $otp1 = $otp1.chr(rand(48,57));
}    

// SMS script here (working fine)

?>

<form action="process.php" method="post">
Enter OTP: <br>
<input type="number" name="otp2" >
<br>
<? php
session_start();
$_SESSION['otpnuser'] = 1;
$_SESSION['secretpassword'] = $otp1;
$_SESSION["otp2"] = $otp1;


?>
<input type="submit">
</form>

process.php

<?php
session_start();
if($_POST["otp2"] == $_SESSION["otp2"])
{
    $_SESSION['otpuser'] = 1;
    header("Refresh: 2; URL=success.php");
}
else
{
    $_SESSION['otpuser'] = 0;
    header("Refresh: 2; URL=fail.php");  
}
?>

即使 OTP 正确,它总是将我重定向到 fail.php 页面。

【问题讨论】:

  • 你在哪里声明 $_SESSION["otp2"] 在你的 index.php 中?
  • $_SESSION["otp2"] = $otp1;应该在 index.php 页面中声明??

标签: php forms session session-variables


【解决方案1】:

if($_POST["otp2"] == $_SESSION["otp2"]) 总是为假,因为你还没有声明/分配任何值 otp2

在你的index.php

session_start();

$_SESSION['otpnuser'] = 1;

$_SESSION['secretpassword'] = $otp1;

$_SESSION["otp2"] = "SOME VALUE";

`

【讨论】:

  • 我在 index.php 中这样做了$_SESSION["otp2"] = $otp1;,以检查 post 值 (otp2) 和 otp1 是否相同。但它仍然无法正常工作。 (代码更新)
  • 在您的 process.php 中将 `$_SESSION['otp2']` 替换为 $_SESSION['secretpassword']
  • 我现在收到了Parse error: syntax error, unexpected ''] == $_SESSION['' (T_CONSTANT_ENCAPSED_STRING), expecting ']' in C:\xamppnew\htdocs\otp\process.php on line 3
猜你喜欢
  • 2011-08-13
  • 2012-10-02
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
相关资源
最近更新 更多