theodorejb / responsive-captcha
Generate mobile-friendly, accessible CAPTCHAs
v3.0.1
2020-08-31 16:36 UTC
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: ^8.5.8
README
Prevent form spam by generating random, accessible arithmetic and logic questions.
Examples:
- "What is the fourth letter in snowboard?"
- "What is the sum of four and six?"
- "What is eight multiplied by two?"
- "Which is smallest: sixty-six, one hundred, or twenty-two?"
Users can respond with either the numeric or textual version of an answer (e.g. "16" or "sixteen").
For background info on this project, see my blog post: https://theodorejb.me/2012/12/30/responsive-captcha/
Install via Composer
composer require theodorejb/responsive-captcha
Usage
-
Generate a random question:
use function theodorejb\ResponsiveCaptcha\{randomQuestion, checkAnswer}; $qa = randomQuestion(); $realAnswer = $qa->getAnswer(); // save somewhere (e.g. in session or encrypted single-use token)
-
Display question in form:
<label> <?= $qa->getQuestion() ?> <input type="text" name="captcha" /> </label>
-
Check whether the user's response is correct:
$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 } }