Mecha CMS

Code snippet collection for developers.

Custom Math Captcha

Table of Content

Playing around with the math captcha.

Requires captcha extension.

Unicode 

Using unicode characters to customize the number appearance:

// → ❷ ➕ ❶
echo Captcha::math(1, 10, [
    1 => '❶',
    2 => '❷',
    3 => '❸',
    4 => '❹',
    5 => '❺',
    6 => '❻',
    7 => '❼',
    8 => '❽',
    9 => '❾',
    10 => '❿',
    '+' => '➕',
    '-' => '➖'
]);
// → ➇ ➖ ➅
echo Captcha::math(1, 10, [
    1 => '➀',
    2 => '➁',
    3 => '➂',
    4 => '➃',
    5 => '➄',
    6 => '➅',
    7 => '➆',
    8 => '➇',
    9 => '➈',
    10 => '➉',
    '+' => '➕',
    '-' => '➖'
]);
// → ➌ ➕ ➎
echo Captcha::math(1, 10, [
    1 => '➊',
    2 => '➋',
    3 => '➌',
    4 => '➍',
    5 => '➎',
    6 => '➏',
    7 => '➐',
    8 => '➑',
    9 => '➒',
    10 => '➓',
    '+' => '➕',
    '-' => '➖'
]);

Image 

Using images to customize the number appearance:

echo Captcha::math(1, 10, [
    1 => '<img src="one.gif">',
    2 => '<img src="two.gif">',
    3 => '<img src="three.gif">',
    4 => '<img src="four.gif">',
    5 => '<img src="five.gif">',
    6 => '<img src="six.gif">',
    7 => '<img src="seven.gif">',
    8 => '<img src="eight.gif">',
    9 => '<img src="nine.gif">',
    10 => '<img src="ten.gif">',
    '+' => '<img src="plus.gif">',
    '-' => '<img src="minus.gif">'
]);

Random 

Random output:

echo Captcha::math(1, 5, [
    1 => array_rand([1, 'one', '&#x2776;', '<img src="one.gif">']),
    2 => array_rand([2, 'two', '&#x2777;', '<img src="two.gif">']),
    3 => array_rand([3, 'three', '&#x2778;', '<img src="three.gif">']),
    4 => array_rand([4, 'four', '&#x2779;', '<img src="four.gif">']),
    5 => array_rand([5, 'five', '&#x277A;', '<img src="five.gif">']),
    '+' => array_rand(['+', 'plus', '&#x2795;', '<img src="plus.gif">']),
    '-' => array_rand(['-', 'minus', '&#x2796;', '<img src="minus.gif">'])
]);

Fake Operator 

Fake math operator:

echo Captcha::math(1, 3, [
    1 => '5 + 3 - 7',
    2 => '(4 / 2)',
    3 => '12 - 9',
    '+' => '+',
    '-' => '-'
]);