theodorejb/responsive-captcha

生成适合移动端且易于访问的 CAPTCHA

v3.0.1 2020-08-31 16:36 UTC

This package is auto-updated.

Last update: 2024-09-12 03:41:53 UTC


README

Packagist Version

通过生成随机、易于访问的算术和逻辑问题来防止表单垃圾邮件。

示例

  • "滑雪板中第四个字母是什么?"
  • "四加六等于多少?"
  • "八乘以二等于多少?"
  • "哪个最小:六十六、一百还是二十二?"

用户可以用数字或文字形式回答(例如 "16" 或 "sixteen")。

有关此项目的背景信息,请参阅我的博客文章:https://theodorejb.me/2012/12/30/responsive-captcha/

使用 Composer 安装

composer require theodorejb/responsive-captcha

使用方法

  1. 生成随机问题

    use function theodorejb\ResponsiveCaptcha\{randomQuestion, checkAnswer};
    
    $qa = randomQuestion();
    $realAnswer = $qa->getAnswer(); // save somewhere (e.g. in session or encrypted single-use token)
  2. 在表单中显示问题

    <label>
        <?= $qa->getQuestion() ?>
        <input type="text" name="captcha" />
    </label>
  3. 检查用户的回答是否正确

    $answer = filter_input(INPUT_POST, "captcha");
    
    if ($answer !== null) {
        if (checkAnswer($answer, $realAnswer)) {
            // code to execute if the captcha answer is correct
        } else {
            // the answer is incorrect - show an error to the user
        }
    }