skip to Main Content

I am using the Mews package as Captcha in my Laravel app.

So I tried installing it via this command and published the assets as well:

composer require mews/captcha

Then in the Blade, I added this:

<div class="form-group">
    <div class="form-group">
        <div class="captcha-container">
            <div>{!! Captcha::img() !!}</div>
            <a href="#" id="refresh-captcha">
            <img src="{{ asset('img/refresh.png') }}" alt="Refresh Captcha">
            </a>
        </div>
        <input type="text" class="form-control" id="captcha" name="captcha" placeholder="نوشته بالا را وارد کنید">
        @error('captcha')
            <span class="text-danger">{{ $message }}</span><br>
        @enderror
    </div>

    @error('captcha')
        <span class="text-danger">{{ $message }}</span><br>
    @enderror
</div>

And it works find and shows several characters as Captcha (around 10 characters).

But I wanted to show only 5 characters and not more than that.

So I changed it this way:

<div class="captcha-container">
    @php
        config(['captcha.length' => 5]); // Set captcha length to 5
        $captchaImage = Captcha::img(); // Generate captcha image
        $captchaCode = substr($captchaImage, strpos($captchaImage, 'value=') + 7, 5); // Extract the first 5 characters
    @endphp
    <div>{!! $captchaImage !!}</div>
    <div>{{ $captchaCode }}</div>
    <a href="#" id="refresh-captcha">
        <img src="{{ asset('img/refresh.png') }}" alt="Refresh Captcha">
    </a>
</div>
            

Then at the config/app.php:

return [
    'secret' => env('NOCAPTCHA_SECRET'),
    'sitekey' => env('NOCAPTCHA_SITEKEY'),
    'options' => [
        'timeout' => 30,
    ],
    'length' => env('CAPTCHA_LENGTH', 5),
];

And in the .env file, I have this:

CAPTCHA_LENGTH=5

Now it should show the captcha with 5 characters but still shows more than that:

capture

So what’s going wron here? How can I only display 5 characters?

2

Answers


  1. The corresponding config key is captcha.default.length not captcha.length.
    Regardless that, if you want to have more controlle and clearity on the config try to run this command :

    php artisan vendor:publish
    

    now you can find the captcha config file in config/ folder so can edit/view all the params you want to edit.

    Login or Signup to reply.
  2. php artisan vendor:publish
    

    use the above code, go to config/captcha.php change length to 5, like so;

    'default' => [
        **'length' => 5,**
        'width' => 150,
        'height' => 36,
        'quality' => 90,
        'math' => false,
        'expire' => 60,
        'encrypt' => false,
    ],
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search