【问题标题】:validate and saving random generated code php验证并保存随机生成的代码 php
【发布时间】:2017-05-14 13:40:56
【问题描述】:

我有这个简单的 php 代码,它生成一个随机代码并将其显示给用户。如果用户插入显示的代码(如验证码),我需要使用文本输入进行验证,但我将使用此脚本在验证后将用户重定向到注册表单页面。代码已生成,但我无法验证,可能是我遗漏了代码中的某些内容?

注意:这不是垃圾邮件预防系统。正如 cmets 中所说,有比这更好的有效解决方案来防止垃圾邮件。这是用户邀请系统的初稿,如果您想对该问题投反对票,请考虑这一点。

<?php 
function randomCode() {
    $code_variable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789";
    srand((double)microtime() * 1000000);
    $i = 0;
    $rand_Code = '';
    while ($i <= 7)
        {
        $num = rand() % 33;
        $tmp = substr($code_variable, $num, 1);
        $rand_Code = $rand_Code . $tmp;
        $i++;
        }

    return $rand_Code;
}
$code = randomCode(); 

if(isset ($_POST['verify'])){

    if($_POST['user_code'] == $code ){
        echo 'Valid code.';
        header('Location: index.php');
    }

}

?>
<form method="post" action="">
<input type="text" name="verification_code" disabled value="<? echo $code;?>">
<input type="text" name="user_code">
<button type="submit" name="verify">Verify code</button>
</form>

【问题讨论】:

  • 我希望这不是为了进入一个实时站点,因为它更糟糕比作为一种安全机制无用
  • 页面生命周期有点混乱
  • 我想在网站上使用它只是为了保护注册表单,这只是我现在拥有的初稿。 @RiggsFolly 页面生命周期是什么意思?
  • 要点:如果您将code 发送到浏览器,那么任何人都可以看到它。使用表单或嗅探连接。作为一种安全机制,这完全是一场灾难。我是否建议您在考虑推出自己的安全功能时采取很好的案例,制作一个完整的 SNAFU 是最简单的事情
  • @RiggsFolly 我将努力添加将代码邮寄给用户的功能,以便他们插入并查看注册表单页面..

标签: php xml forms validation


【解决方案1】:

你永远不会得到 Valid code. 显示在屏幕上,因为每次用户点击 Verify code 按钮时,页面都会刷新并且一个新的随机验证码将生成并存储在$code 中。所以这就是为什么$_POST['user_code'] 永远不会等于$code

一种解决方法是在 URL 本身中附加验证码,以便您可以通过将其与 $_GET[...] 数据进行比较来验证用户输入的验证码的真实性。所以你需要通过以下方式改变你的表单,

<form method="post" action="?captcha=<?php echo $code; ?>">
    <input type="text" name="verification_code" disabled value="<?php echo $code;?>">
    <input type="text" name="user_code">
    <button type="submit" name="verify">Verify code</button>
</form>

接下来,通过以下方式验证验证码,

// your code
if(isset ($_POST['verify'])){
    if($_POST['user_code'] == $_GET['captcha'] ){
        // your code
    }
}

旁注:

  • 不要在header(...); 语句之前输出任何内容,否则您会看到headers already sent 错误。通过this SO thread 获取更多信息。
  • 单独使用header(...); 不足以将用户重定向到其他页面,请在header(...); 语句之后使用exit();

【讨论】:

  • 好的,我会试试这个技巧,谢谢。关于如何在用户输入代码之前将代码邮寄给用户的任何建议?用户@RiggsFolly 说这种类型的安全措施实施起来是个坏主意。所以我想在用户插入电子邮件的地方添加一个邮件输入,并让脚本生成的代码输入。
  • @BrP 不要重新发明轮子。使用现有的验证码解决方案之一,例如Google reCaptcha
  • 在这里!在您获得安全博士学位之前,绝对不要发明安全机制。绝对要等到您至少了解 Web 表单的生命周期之后。 记住 WEP
  • @BrP 关于如何邮寄代码 ...? 您可以使用以下任一解决方案, 1. PHP 的原生@ 987654323@函数2.PHP Mailer3.SendGrid API
  • @BrP 该解决方案仅适用于实时站点,不适用于您的本地主机。将 组合保存在您的数据库中,然后向用户的电子邮件地址发送一封邮件,链接如下:dummyexample.com/?email=user@email.com&captcha=captchacode。所以当用户收到这封邮件并点击链接时,根据邮件地址$_GET['email'],从数据库中检索对应的验证码,并与$_GET['captcha']进行比较。
【解决方案2】:

我添加了 php mail() 函数来向用户发送邀请码。它工作正常,我已将代码分为两部分。第一个管理用户邀请码发送:

<form method="post" action="?invitation=<?php echo $code; ?>">
<label for="email">Email</label>    
<input type="text" name="email">
<button type="submit" name="send_invitation">Send invitation code</button>
</form>


<?php
session_start();
ob_start();

if(isset($_POST['send_invitation'])){

function randomCode()
{
$code_variable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789";
srand((double)microtime() * 1000000);
$i = 0;
$rand_Code = '';
while ($i <= 7)
    {
    $num = rand() % 33;
    $tmp = substr($code_variable, $num, 1);
    $rand_Code = $rand_Code . $tmp;
    $i++;
    }

return $rand_Code;
}
$_SESSION['code'] = randomCode();
$_SESSION['tmp_mail'] = $_POST['email'];



if(isset($_POST['verify'])){
$to = $_POST['email']; // this is your Email address
$from = "noreply@domain.com"; // this is the sender's Email address
$subject = "Affiliate invitation code";
$message = $_SESSION['code'] . " " . " is your invitation code.";


$headers = "From:" . $from;

mail($to,$subject,$message,$headers);

echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
?>

脚本的第二部分在另一个页面上,管理存储在 $_SESSION['code'] 变量中的代码与用户输入之间的代码匹配检查

<?php 
session_start();
ob_start();
function redirect(){
header('refresh:3; url=affiliate/index.php');
exit;
}

if(isset($_POST['verify'])){

if($_POST['user_code'] == $_SESSION['code']){    
echo "Valid invitation code. You can now register using $_SESSION['tmp_mail'] email address.";
} else { echo "Wrong code. Please enter a valid code or request a new one."; }
?>
<form method="post" action="?invitation=<?php echo $code; ?>">
<label for="Code">Invitation Code</label>    
<input type="text" name="user_code">
<button type="submit" name="verify">Send invitation code</button>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多