Skip to content

Commit

Permalink
Add basic "sum" captcha.
Browse files Browse the repository at this point in the history
  • Loading branch information
Firefishy committed Oct 6, 2023
1 parent b5f5202 commit b0f3bba
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@

require __DIR__ . '/vendor/autoload.php';

if (!isset($_SESSION['number1'])) {
$_SESSION['number1'] = (int) rand(1,9);
$_SESSION['number2'] = (int) rand(1,9);
}

function dummy_captcha_validate($field) {
if (isset($_SESSION['number1'])) {
if (isset($_SESSION['number2'])) {
$sum = $_SESSION['number1'] + $_SESSION['number2'];
if ($field == $sum) {
return true;
}
}
}
return false;
}

function process_data ($values) {
$mail = new PHPMailer(true);
try {
Expand Down Expand Up @@ -125,6 +142,11 @@ function process_data ($values) {
$form->addElement('text', 'signature', 'Typing your full name in this box will act as your digital signature.', array('size' => 25, 'maxlength' => 255));
$form->addRule('signature', 'Field is required', 'required', null, 'client');

$form->registerRule('captcha_validate', 'callback', 'dummy_captcha_validate');
$form->addElement('text', 'captcha', 'Please add (+) together the following numbers: ' . $_SESSION['number1'] . ' and ' . $_SESSION['number2'], array('size' => 25, 'maxlength' => 255));
$form->addRule('captcha', 'Captcha field is required', 'required', null, 'client');
$form->addRule('captcha', 'You did not solve the sum', 'captcha_validate');

$form->addElement('submit', null, 'Send');

$form->removeAttribute('name');
Expand Down

0 comments on commit b0f3bba

Please sign in to comment.