This library is part of mobiCMS Content Management System, but can be used as an independent library in other projects. Prevent form spam by generating random Captcha images.
composer require mobicms/mobicms-captcha
-
Generating code:
$captcha = new Mobicms\Captcha\Captcha; $code = $captcha->generateCode(); $_SESSION['code'] = $code;
-
Display in form:
<form method="post"> <!-- ... --> <img alt="Verification code" width="<?= $captcha->width ?>" height="<?= $captcha->height ?>" src="<?= $captcha->generateImage($code) ?>" > <input type="text" size="5" name="code"> <!-- ... --> </form>
-
Check whether the entered code is correct:
$result = filter_input(INPUT_POST, 'code'); $session = filter_input(INPUT_SESSION, 'code'); if ($result !== null && $session !== null) { if (strtolower($result) == strtolower($session)) { // CAPTCHA code is correct } else { // CAPTCHA code is incorrect, show an error to the user } }