Skip to content

mobicms/captcha

Repository files navigation

mobicms/captcha

GitHub GitHub release (latest SemVer) Packagist

CI-Analysis CI-Tests Sonar Coverage Quality Gate Status

This library is a simple PHP CAPTCHA. Prevent form spam by generating random Captcha images.

Major features:

  • lightweight and fast
  • not create any temporary files
  • there are many settings that allow you to change the look of the picture
  • you can use your own font sets

Example:

Captchas examples

Installation

The preferred method of installation is via Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:

composer require mobicms/captcha

Usage

  • Display in form:
<?php
$code = (string) new Mobicms\Captcha\Code;
$_SESSION['code'] = $code;
?>

<form method="post">
<!-- ... -->
<img alt="Verification code" src="<?= new Mobicms\Captcha\Image($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
    }
}

Contributing

Contributions are welcome! Please read Contributing for details.

YAGNI KISS

In our development, we follow the principles of YAGNI and KISS. The source code should not have extra unnecessary functionality and should be as simple and efficient as possible.

License

This package is licensed for use under the MIT License (MIT).
Please see LICENSE for more information.

Our links