Home

Awesome

simple-captcha

Simple, image-based, mathematical captcha, with increasing levels of difficulty for PHP, JavaScript, Python

version 2.6.0

SimpleCaptcha

see also:

Example:

  1. Captcha with Position Distortion: Captcha with Position Distortion
  2. Captcha with Scale Distortion: Captcha with Scale Distortion
  3. Captcha with Gradient Background: Captcha with Gradient Background
  4. Captcha with custom pattern background: Captcha custom pattern background
// setup
$captcha = (new SimpleCaptcha())
    ->option('secret_key', 'SECRET_KEY')
    ->option('secret_salt', 'SECRET_SALT_')
    ->option('difficulty', 1) // 0 (easy) to 3 (difficult)
    ->option('distortion_type', 1) // 1: position distortion, 2: scale distortion
    ->option('num_terms', 2)
    ->option('max_num_terms', 4) // -1 means constant num_terms
    ->option('min_term', 1)
    ->option('max_term', 21)
    ->option('has_multiplication', true)
    ->option('has_division', true)
    ->option('has_equal_sign', true)
    ->option('color', 0x121212)
    ->option('background', 0xffffff)
;
<!-- use it -->
<?php $captcha->reset(); ?>
<form action="/validate" method="post">
<!-- you can store the captcha hash in the $_SESSION or in $_COOKIE as well -->
<input type="hidden" name="hash" value="<?php echo $captcha->getHash(); ?>" />
Compute result <img src="<?php echo $captcha->getCaptcha(); ?>" /> <input type="text" name="answer" value="" />
<button type="submit">Submit</button>
</form>
// use it
$app->on('/validate', function() use ($captcha) {
    // you can store the captcha hash in the $_SESSION or in $_COOKIE as well
    if ($captcha->validate($_POST['answer'], $_POST['hash']))
    {
    // correct captcha
    }
    else
    {
    // wrong captcha
    }
});