lorddashme / php-simple-captcha
A simple captcha package that fit to any type of web application built on php.
Installs: 1 779
Dependents: 0
Suggesters: 0
Security: 0
Stars: 11
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=5.6 || >=7.0 || >=7.1 || >=7.2
- lorddashme/php-static-class-interface: 1.*
Requires (Dev)
- mockery/mockery: 1.*
- phpunit/phpunit: 5.* || 6.* || 7.*
This package is auto-updated.
Last update: 2024-10-30 01:42:45 UTC
README
A simple captcha package that fit to any type of web application built on php.
Sample
Requirement(s)
- PHP version from 5.6.* up to latest.
Install
- Recommended to install using Composer. Use the command below to install the package:
composer require lorddashme/php-simple-captcha
Usage
List of Available Functions
- Basic usage:
<?php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\SimpleCaptcha\Captcha; // Initialize the captcha class. $captcha = new Captcha(); // Execute the random generation of code. $captcha->code(); // Execute the image captcha rendering. $captcha->image(); // The generated captcha code, something like "QwErTyx..." echo $captcha->getCode(); // The generated captcha image that include the code above. // The output is base64 data image "data:image/png;base64,iVBORw0KGgoAA..." echo $captcha->getImage();
- Also can be done by using the code below:
<?php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\SimpleCaptcha\Facade\Captcha; // Initialize the facade captcha class. Captcha::init(); // Execute the random generation of code. Captcha::code(); // Execute the image captcha rendering. Captcha::image(); // The generated captcha code, something like "QwErTyx..." echo Captcha::getCode(); // The generated captcha image that include the code above. // The output is base64 data image "data:image/png;base64,iVBORw0KGgoAA..." echo Captcha::getImage();
Captcha Session Usage
-
The package also provided a simple way to validate the user input code, for example we have a login feature.
-
Login Page:
-
Initialize the Captcha class together with the code and image generation process.
-
Use the
storeSession()
to store the generated details in the captcha session. -
The stored session details are essential for validating the user input later on.
<?php // login.php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\SimpleCaptcha\Captcha; $captcha = new Captcha(); $captcha->code(); $captcha->image(); $captcha->storeSession(); ?> <form action="validate-login.php" method="POST"> ... <img src="<?php echo $captcha->getImage(); ?>"> <input type="text" name="user_captcha_code" value=""> <input type="submit" value="Login"> </form>
-
-
Validation Route:
-
We need to initialize again the Captcha class but now we don't need to initialize the code and image generation.
-
Validate the user inputed captcha code.
<?php // validate-login.php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\SimpleCaptcha\Captcha; $captcha = new Captcha(); $data = $captcha->getSession(); // return array( 'code' => 'QwErTyx...' ) if ($_POST['user_captcha_code'] === $data['code']) { return 'Code is valid!'; } else { return 'Code is invalid!'; }
-
-
You may also check the sample in the root directory of the package that will show you the actual example of implementing captcha class.
-
Captcha Configuration
- To change the default config of the class:
<?php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\SimpleCaptcha\Captcha; use LordDashMe\SimpleCaptcha\Facade\Captcha as CaptchaFacade; $config = array( 'session_name' => 'ldm-simple-captcha', 'session_index_name' => 'LDM_SIMPLE_CAPTCHA', 'session_https' => false, 'session_http_only' => true, 'font_color' => '#000', 'font_size_min' => 26, 'font_size_max' => 28, 'angle_min' => 0, 'angle_max' => 9, 'shadow' => true, 'shadow_color' => '#fff', 'shadow_offset_x' => -3, 'shadow_offset_y' => 1, 'backgrounds' => array( 'bg1.png', 'bg2.png', 'bg3.png', 'bg4.png', 'bg5.png', 'bg6.png', 'bg7.png', 'bg8.png' ), 'fonts' => array( 'capsmall_clean.ttf' ) ); $captcha = new Captcha($config); // Or you can use this style. CaptchaFacade::init($config);
Tips
-
Overriding the default config:
-
The
backgrounds
andfonts
are tightly coupled in the directory structure of the package. -
If you want to override the
backgrounds
andfonts
you need to extends the Captcha class with your new sub class and override the protected methods of Captcha class for resources directory,backgroundsDirectoryPath()
andfontsDirectoryPath
.<?php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\SimpleCaptcha\Captcha; class MyNewCaptcha extends Captcha { public function __construct($config = array()) { parent::__construct($config); } protected function backgroundsDirectoryPath() { return 'path/to/your/custom/backgrounds/'; } protected function fontsDirectoryPath() { return 'path/to/your/custom/fonts/'; } }
-
License
This package is open-sourced software licensed under the MIT license.