Skip to content

Latest commit

 

History

History
82 lines (62 loc) · 2.26 KB

README.md

File metadata and controls

82 lines (62 loc) · 2.26 KB

Laravel Captcha

Laravel Captcha Generator

Requirements

  • PHP 5.6+
  • Laravel Framework 5.2+
  • GD Library enabled

Installation

  • Run
composer require zerosdev/laravel-captcha:dev-master
  • Add this code to your config/app.php in providers array
ZerosDev\LaravelCaptcha\ServiceProvider::class,
  • Add this code to your config/app.php in aliases array
'Captcha' => ZerosDev\LaravelCaptcha\Facade::class,
  • Run
composer dump-autoload
  • Run
php artisan vendor:publish --provider="ZerosDev\LaravelCaptcha\ServiceProvider"

Configuration

Open your config/captcha.php and adjust the setting. Optionally, you can adjust some captcha setting on-the-fly with available methods below:

chars(String)

Set the characters that will be used as captcha text

size(Integer width, Integer height)

Set the captcha image width and height in pixel (px)

length(Integer)

Set the captcha character length

Generation Example


// Import from root class (Captcha Facade)
use Captcha;

// generate captcha
$captcha = Captcha::chars('123456789ABCDEFGHIJKLMNPQRSTUVWXYZ')->length(4)->size(120, 40)->generate();

// get captcha id
$id = $captcha->id();
// return random generation id

// print html hidden form field, used in blade template
{{ $captcha->form_field() }}
// return: <input type="hidden" name="captcha_id" value="XXXXXXXXXXXXXX" id="captcha_id">

// get base64 image
$image = $captcha->image();
// return data:image/png; base64,XXXXXXXXXXXXXX

// print html image, used in blade template
{{ $captcha->html_image(['onclick' => 'jsFunction()', 'style' => 'border:1px solid #ddd']) }}
// return: <img src="data:image/png; base64,XXXXXXXXXXXXXX" onclick="jsFunction()" style="border:1px solid #ddd" />

Validation


// Import from root class (Captcha Facade)
use Captcha;

// validate captcha
// $captchaId = Captcha generation id, $captcha->id()
// $captchaText = Captcha input from client request

if( Captcha::validate($captchaId, $captchaText) )
{
    // Valid
}
else
{
    // Invalid
}

Advise

It is recommended to avoid using the "0" (zero) and "O" characters in captcha to avoid being ambiguous