Giter Club home page Giter Club logo

laravel-captcha's Introduction

Captcha integration for Laravel

Latest Stable Version Total Downloads License

example1 example2 example3 example4 example5 example6 example7 example8 example9 example10 example11 example12

Installing Laravel Captcha Composer Package

Note: If you do not have Composer yet, you can install it by following the instructions on https://getcomposer.org

Step 1. Install package

composer require bonecms/laravel-captcha
Captcha Laravel
v2.2 5.5 - 9.x
v2.1 5.4
v1.1 5.3 and below

Step 2 for Laravel 5.5 and below. Register the Laravel Captcha service provider

{LARAVEL_ROOT}/config/app.php:

'providers' => [
    ...
    Igoshev\Captcha\Providers\CaptchaServiceProvider::class,
],

Using Laravel Captcha

Generate a Captcha markup in your Controller:

<?php 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class MyController extends Controller 
{
    public function getExample() 
    {
        return view('myView');
    }

}

Showing a Captcha in a View:

...
@captcha
<input type="text" id="captcha" name="captcha" autocomplete="off">
 ...

Check user input during form submission:

<?php 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class MyController extends Controller 
{
    public function getExample() 
    {
        return view('myView');
    }

    public function postExample(Request $request)
    {
    	$this->validate($request, [
            'captcha' => 'required|captcha'
        ]);

        // Validation passed
    }
}

Configuration

php artisan vendor:publish --tag=bone-captcha-config
<?php

return [

/*
    |--------------------------------------------------------------------------
    | Captcha middleware
    |--------------------------------------------------------------------------
    |
    */
    'middleware' => ['web'],

    /*
    |--------------------------------------------------------------------------
    | Captcha routes
    |--------------------------------------------------------------------------
    |
    */
    'routes' => [
        'image'     => 'captcha/image',
        'image_tag' => 'captcha/image_tag'
    ],

    /*
    |--------------------------------------------------------------------------
    | Blade directive
    |--------------------------------------------------------------------------
    | You can use blade directive @captcha for rendering captcha.
    |
    */
    'blade' => 'captcha',

    /*
    |--------------------------------------------------------------------------
    | Validator name
    |--------------------------------------------------------------------------
    |
    */
    'validator' => 'captcha',

    /*
    |--------------------------------------------------------------------------
    | Captcha generator.
    |--------------------------------------------------------------------------
    | Must implement GeneratorInterface.
    |
    */
    'generator' => \Igoshev\Captcha\Captcha\Generator\GeneratorWaves::class,

    /*
    |--------------------------------------------------------------------------
    | Storage code.
    |--------------------------------------------------------------------------
    | Must implement StorageInterface.
    |
    */
    'storage' => \Igoshev\Captcha\Captcha\Storage\SessionStorage::class,

    /*
    |--------------------------------------------------------------------------
    | Code generator.
    |--------------------------------------------------------------------------
    | Must implement CodeInterface.
    |
    */
    'code' => \Igoshev\Captcha\Captcha\Code\SimpleCode::class,

    /*
    |--------------------------------------------------------------------------
    | Font
    |--------------------------------------------------------------------------
    | Supported: "IndiraK".
    |
    */
    'font' => base_path('vendor/bonecms/laravel-captcha/src/resources/fonts/IndiraK.ttf'),

    /*
    |--------------------------------------------------------------------------
    | Font size
    |--------------------------------------------------------------------------
    | Font size in pixels.
    |
    */
    'fontSize' => 26,

    /*
    |--------------------------------------------------------------------------
    | Letter spacing
    |--------------------------------------------------------------------------
    | Spacing between letters in pixels.
    |
    */
    'letterSpacing' => 2,

    /*
    |--------------------------------------------------------------------------
    | Code Length
    |--------------------------------------------------------------------------
    | You can specify an array or integer.
    |
    */
    'length' => [4, 5],

    /*
    |--------------------------------------------------------------------------
    | Displayed chars
    |--------------------------------------------------------------------------
    | Enter the different characters.
    |
    */
    'chars' => 'QSFHTRPAJKLMZXCVBNabdefhxktyzj23456789',

    /*
    |--------------------------------------------------------------------------
    | Image Size
    |--------------------------------------------------------------------------
    | Captcha image size can be controlled by setting the width
    | and height properties.
    |
    |
    */
    'width'  => 180,
    'height' => 50,

    /*
    |--------------------------------------------------------------------------
    | Background Captcha
    |--------------------------------------------------------------------------
    | You can specify an array or string.
    |
    */
    'background' => 'f2f2f2',

    /*
    |--------------------------------------------------------------------------
    | Colors characters
    |--------------------------------------------------------------------------
    | You can specify an array or string.
    |
    */
    'colors' => '2980b9',

    /*
    |--------------------------------------------------------------------------
    | Scratches
    |--------------------------------------------------------------------------
    | The number of scratches displayed in the Captcha.
    |
    */
    'scratches' => [1, 6],

    /*
    |--------------------------------------------------------------------------
    | Id of the Captcha code input textbox
    |--------------------------------------------------------------------------
    | After updating the Captcha focus will be set on an element with this id.
    |
    */
    'inputId' => 'captcha',
    
];

Localization

Supported languages (17):

  • Arabic
  • Chinese
  • Dutch
  • English
  • French
  • German
  • Hindi
  • Indonesian
  • Italian
  • Japanese
  • Korean
  • Persian
  • Portuguese
  • Russian
  • Spanish
  • Turkish
  • Ukrainian
php artisan vendor:publish --tag=bone-captcha-lang

View

php artisan vendor:publish --tag=bone-captcha-views

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

paypal

laravel-captcha's People

Contributors

amannim98 avatar benancetin avatar bennychan avatar braunson avatar c-dumur avatar fatelgit avatar igoshev avatar javierpacareu avatar jorgeandd avatar jschram avatar mazedlx avatar michaylov avatar paolobarbolini avatar samsambabadi avatar tm1000 avatar visavi avatar zymawy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-captcha's Issues

Error

Class bone_captcha does not exist

when setting the view template, it still takes the data for the template from the vendor

when setting the view template, it still takes the data for the template from the vendor

`


<img src="/captcha/image?_=568421238"
alt="https://github.com/igoshev/laravel-captcha"
style="cursor:pointer;width:180px;height:50px;"
title="Update"
onclick="this.setAttribute('src','/captcha/image?=568421238&='+Math.random());var captcha=document.getElementById('captcha');if(captcha){captcha.focus()}"

    <input type="text" id="captcha" name="captcha" autocomplete="off">
</form>`

Cannot validate captcha

Hello

First I would like to thank you for creating this very useful library.
So I tried using your library it shows the captcha but when I validate it fails,
did I miss out something?
I followed all the steps.

It seems the captcha code that was saved in the session was not retained during validation.

I am using Laravel 5.3

Regards,
Myles

Validator::extend(....) not working

....\bonecms\laravel-captcha\src\Providers\CaptchaServiceProvider.php
protected function registerValidator() { Validator::extend(config('bone.captcha.validator'), function ($attribute, $value, $parameters, $validator) { return $this->app[Captcha::class]->validate($value); }, trans('bone::captcha.incorrect_code')); }
This code not working for me

This code variant works:
protected function registerValidator() { Validator::extendImplicit(config('bone.captcha.validator'), function ($attribute, $value, $parameters, $validator) { return $this->app[Captcha::class]->validate($value); }, trans('bone::captcha.incorrect_code')); }

Translation not work properly

Hi
I installed this package and published translation
Captcha and validation works perfectly but translation works only in view
Validation error is in default language (en)
I've changed the registerValidator in service provider to find the problem and it returns the locale is en
/** * Register captcha validator. */ protected function registerValidator() { Validator::extend(config('bone.captcha.validator'), function ($attribute, $value, $parameters, $validator) { return $this->app[Captcha::class]->validate($value); }, \App::getLocale()); }

The same Captcha code can be used unlimited times (defeating purpose of captcha)

If you manually fill out the captcha and submit the form, as long as you don't load a new captcha, you can then use the same captcha code to post to the same URL unlimited times.

It seems like the validator for the Captcha should also clear the bone_captcha session variable after validating the captcha, to avoid allowing the same code to be allowed multiple times.

As a workaround for my current project, I just manually wipe the bone_captcha field after it validates successfully. Then the user is required to load the captcha image before supplying the code (which makes a lot of sense).

If for some reason it's difficult to clear the bone_captcha session variable in the validator (I suppose this might be a backwards incompatibility issue, in the case that a project validates the captcha multiple times in a single request for some reason), then it should at least include in the examples in the readme that the bone_captcha session vairable must be wiped after a successful validation.

Very long time to show

Captcha show with very long time, i don't know why, but this is using localhost with xampp and php 8.1
image

Could not load image

Laravel ver. 5.4.36

After, following all the necessary steps - the captcha image outputs the alternate text, and inspector shows 'could not load image'.

Could this be a bug? or did I miss out on anything?

Added to provider hence, facade access.
Added config file.
Able to use captcha html output.

But failed to display, captcha image and outputs Alt.

Mocking CAPTCHA

Hello,
Is there any way to mock the CAPTCHA in an unit for feature test?
Thanks!

Captcha image not load because of IndiraK.ttf font file

when I'm try to show the captcha in view by @captcha directive it throws error exception:
ErrorException: imagettftext(): Could not find/open font in file neelain_certificates/vendor/bonecms/laravel-captcha/src/Captcha/Generator/GeneratorWaves.php on line 28

If I comment the font line in config file the error goes away.
'font' => base_path('vendor/bonecms/laravel-captcha/src/resources/fonts/IndiraK.ttf')

Image staore in server

Hi , thanks for all the efforts contributed to build up this awesome package.
I was wondering where do these captcha image store, its store storage folder ? thanks

Validation fails when using Validator::passes() or Validator::fails()

First of all thank you for this great and easy to use captcha package.

VERSION
PHP version 7.1.22
Laravel version 5.4
Package version 2.1.0

ISSUE
Captcha validation fails when using $validator->passes() or $validator->fails() inside a FormRequest class. I have a request validation class which always throws the 'Invalid code from the image' error

namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;

class MyRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
            'captcha' => 'required|captcha',
        ];
    }

    public function withValidator($validator)
    {
        if($validator->passes())
        {
            //do something
        }
    }
}

LIKELY CAUSE
Running $validator->passes() or $validator->fails() runs the validation again, but the captcha session is likely cleared in the first validation which causes the validation to fail when run for the second time.

The directive name [] is not valid. Directive names must only contain alphanumeric characters and underscores.

  • Laravel Captcha v2.2.4
  • Laravel v8.26.1
  • PHP 8.0

I get the following error when installing this package:
The directive name [] is not valid. Directive names must only contain alphanumeric characters and underscores.

But, I already found a workaround for this.

How to fix this:

  1. If you already installing the package with composer require bonecms/laravel-captcha, then you need to uninstall the package first with composer remove bonecms/laravel-captcha
  2. Remove the cached configuration with php artisan config:clear.
  3. Install the package, composer require bonecms/laravel-captcha
  4. Re-cache the config, php artisan config:cache
  5. Done.

References: https://stackoverflow.com/questions/65464010/the-directive-name-is-not-valid-directive-names-must-only-contain-alphanumer

Renew code if code expired

if code expired, just wrong captcha error showed. there is two solution for this problem. first is custom validation error for expired code and second renew automatically code if code expired.

thanks for clean code

php 8.1 issue

I just updated my production server to php 8.1 and noticed, that the captcha is not working correctly anymore. For some reason it works locally, but takes about 5-8 seconds to load. Had no issue with php 8.0 before.

On my live server, I get the following exception and no image is being generated:

production.ERROR: Call to undefined function Igoshev\Captcha\Captcha\Generator\ImageTTFtext() {"exception":"[object] (Error(code: 0): Call to undefined function Igoshev\Captcha\Captcha\Generator\ImageTTFtext() at /home/xxx/webapps/xxx/releases/20220128154920/vendor/bonecms/laravel-captcha/src/Captcha/Generator/GeneratorWaves.php:30)

Screenshot 2022-01-28 at 16 52 35

Screenshot 2022-01-28 at 16 56 17

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.