PHP code example of nguyendachuy / laravel-recaptcha3

1. Go to this page and download the library: Download nguyendachuy/laravel-recaptcha3 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nguyendachuy / laravel-recaptcha3 example snippets


return [
    /*
    |--------------------------------------------------------------------------
    | The reCAPTCHA site key provided by Google
    |--------------------------------------------------------------------------
    |
    | Here you can set the sitekey
    */

    'sitekey' => env('GOOGLE_CAPTCHA_SITEKEY', null),

    /*
    |--------------------------------------------------------------------------
    | The reCAPTCHA secret key provided by Google
    |--------------------------------------------------------------------------
    |
    | Here you can set the secet
    */

    'secret' => env('GOOGLE_CAPTCHA_SECRET', null)
];

use NguyenHuy\Recaptcha\Facades\Recaptcha;

class ContactController extends Controller
{
    public function store(Request $request)
    {
        // Verify the token
        $token = $request->input('g-recaptcha-response');
        $ip = $request->ip();
        $action = 'contact_form';
        $minScore = 0.5;
        
        $result = Recaptcha::verify($token, $ip, [$action, $minScore]);
        
        if (!$result) {
            return back()->withErrors(['captcha' => 'reCAPTCHA verification failed']);
        }
        
        // Process form submission
        // ...
    }
}

<body>
    {{-- your app --}}

    {{-- Default action is "form" --}}
    @recaptchaJs

    {{-- or custom action --}}
    @recaptchaJs('form')
</body>

<form>
    {{-- your input --}}

    {{-- Default name is "g-recaptcha-response" --}}
    @recaptchaInput

    {{-- or custom name --}}
    @recaptchaInput('custom-name')
    
    {{-- with custom attributes and action --}}
    @php
        $attributes = ['data-form' => 'contact'];
        $action = 'contact_form';
        $name = 'g-recaptcha-response';
    @endphp
    @recaptchaInput($name)
</form>

$request->validate([
    'g-recaptcha-response' => 'captcha'
]);

php artisan view:clear

$attributes = ['data-form' => 'contact', 'class' => 'recaptcha-input'];
$captcha = new \NguyenHuy\Recaptcha\CaptchaV3();
$display = $captcha->display('g-recaptcha-response', $attributes);

// In your form
@php
    $action = 'contact_form';
@endphp
@recaptchaInput('g-recaptcha-response')

// In your controller
$request->validate([
    'g-recaptcha-response' => 'captcha:contact_form,0.7'
]);
bash
php artisan vendor:publish --tag="recaptcha-config"