Blog Detail

31

Oct
Set up & Validate Friendly Captcha & Response in Laravel App cover image

arrow_back Set up & Validate Friendly Captcha & Response in Laravel App

FriendlyCaptcha is a package that helps in setting up and validating the Friendly Captcha widget and response in your Laravel applications.

Installation

You can install the package via composer:

composer require ossycodes/friendlycaptcha

Configuration

Add FRIENDLY_CAPTCHA_SECRET and FRIENDLY_CAPTCHA_SITEKEY in .env file :

FRIENDLY_CAPTCHA_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
FRIENDLY_CAPTCHA_SITEKEY=XXXXXXXXXXXXXXXX

You can obtain your site-key from here and secret from here.

Usage

Include the FriendlyCaptcha widget scripts in your layout file using the @friendlyCaptchaRenderWidgetScripts Blade directive. This should be added to the head of your document.

<html>
    <head>
        @friendlyCaptchaRenderWidgetScripts()
    </head>
    <body>
        {{ $slot }}
    </body>
</html>

or if you don’t want to use the Blade directive you can do this instead

 {!! FriendlyCaptcha::renderWidgetScripts() !!}

You have two options on how to add the script tag either from unpkg (default) or from jsdelivr

@friendlyCaptchaRenderWidgetScripts() or @friendlyCaptchaRenderWidgetScripts('jsdelivr')
{!! FriendlyCaptcha::renderWidgetScripts() !!} or {!! FriendlyCaptcha::renderWidgetScripts('jsdelivr') !!}

Once that’s done, you can call the renderWidget() method in form to output the appropriate markup (friendlycaptcha widget) with your site key configured.

<form action="/" method="POST">

    {!! FriendlyCaptcha::renderWidget() !!}

    or with custom theme

    {!! FriendlyCaptcha::renderWidget(['dark-theme' => true]) !!}

    or with custom language

    {!! FriendlyCaptcha::renderWidget(['data-lang' => 'en']) !!}

    <button>
        Submit
    </button>
</form>

Finally, On the server, use the provided validation rule to validate the CAPTCHA response.

use Illuminate\Validation\Rule;

public function submit(Request $request)
{
    $request->validate([
        'frc-captcha-solution' => ['required', Rule::friendlycaptcha()],
    ]);
}

For more details, please visit Github.

Note

The team of Codebrisk Laravel developers is always ready to execute even your boldest ideas. Our expert team can design and develop any type of custom CRM solution, SAAS app, or e-commerce app to meet our customer’s needs and transform our customer’s experiences. Get in touch with our team to discuss your bespoke ideas and learn more about the next steps to launching cooperation.

Published at : 31-10-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project