如何在没有gd的联系表格中添加验证码

时间:2012-09-14 08:14:58

标签: php forms security captcha contact

我想请一些帮助。

我有一个联系表单,其中我想添加一些验证码, NO GD Library 。我添加了一些图像作为指南,但现在有静态。我要做的是在循环中输出与隐藏字段中的代码(随机数)匹配的图像,例如,如果随机数为“18301”将显示每个数字的相应图像,这里到目前为止是我的代码:

<label for="captcha">Type the code you see (*):<br />
    <?php (do a loop here){ ?>
<img src="images/0<?php echo $num; ?>.gif" width="18" height="30" />
<?php } ?>  

     <img src="images/00.gif" width="18" height="30" />
         <img src="images/01.gif" width="18" height="30" />
         <img src="images/08.gif" width="18" height="30" />
         <img src="images/03.gif" width="18" height="30" />
         <img src="images/00.gif" width="18" height="30" />
         <img src="images/01.gif" width="19" height="30" /> =</label>
    <span id="spryCaptcha">
    <input type="text" name="captcha" id="captcha" tabindex="70" />
    <input name="hiddenCode" type="hidden" value="<?php echo rand(000000,999999); ?>"/>

我如何才能使这个工作,以便每次都能显示正确的图像并检查用户的输入是否与他/她每次看到的代码相匹配?

2 个答案:

答案 0 :(得分:0)

你应该在循环之前初始化随机数:

<?php
    $random = (string)rand(0,999999);
?>
<label for="captcha">Type the code you see (*):<br />
    <?php for($i=0; $i<strlen($random); $i++){ ?>
        <img src="images/0<?php echo $random{$i}; ?>.gif" width="18" height="30" />
    <?php } ?>  
</label>
<span id="spryCaptcha">
<input type="text" name="captcha" id="captcha" tabindex="70" />
<input name="hiddenCode" type="hidden" value="<?php echo $random ?>"/>

但你应该在会话中保存它并在之后检查(因为隐藏字段是0安全性):

在页面顶部调用session_start()

<?php
    $random = (string)rand(0,999999);
    $_SESSION['captcha'] = $random;
?>
<label for="captcha">Type the code you see (*):<br />
    <?php for($i=0; $i<strlen($random); $i++){ ?>
        <img src="images/0<?php echo $random{$i}; ?>.gif" width="18" height="30" />
    <?php } ?>  
</label>
<span id="spryCaptcha">
<input type="text" name="captcha" id="captcha" tabindex="70" />

并针对$_POST['captcha']

检查$_SESSION['captcha']的下一页

仍然是0安全性,因为可以读取图像名称。

答案 1 :(得分:0)

尝试使用 reCAPTCHA 。它可以与GD库一起使用。 它从外部服务加载验证码图像,并使用公钥加密验证。 所以内部生成验证码图像。 非常好用。

<强> E.G:

前端:

<html>
    <body> <!-- the body tag is required or the CAPTCHA may not show on some browsers -->
      <!-- your HTML content -->

      <form method="post" action="verify.php">
        <?php
          require_once('recaptchalib.php');
          $publickey = "your_public_key"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>
        <input type="submit" />
      </form>

      <!-- more of your HTML content -->
    </body>
  </html>

后端验证:

<?php
  require_once('recaptchalib.php');
  $privatekey = "your_private_key";
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
  } else {
    // Your code here to handle a successful verification
  }
  ?>

了解更多信息请访问:https://developers.google.com/recaptcha/docs/php