【问题标题】:how to add captcha in a contact form with NO gd如何在没有 gd 的联系表单中添加验证码
【发布时间】:2012-09-07 09:12:57
【问题描述】:

我需要一些帮助。

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

<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); ?>"/>

我怎样才能做到这一点,以便每次都会出现正确的图像并检查用户的输入是否与他/她每次看到的代码相匹配??

【问题讨论】:

  • 如果垃圾邮件机器人读取您的图像文件名怎么办?
  • 大声笑,你当前的实现很糟糕
  • 验证码的静态图像?没门!!! (或生成数百万个随机名称的验证码......)

标签: php forms security captcha contact


【解决方案1】:

尝试使用 reCAPTCHA。它可以在没有 GD 库的情况下使用。 它从外部服务加载验证码图像并使用公钥加密进行验证。 所以 NO 内部生成验证码图像。 非常容易使用。

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

【讨论】:

    【解决方案2】:

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

    <?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 安全性,因为可以读取图像名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多