To use Google Captcha in Laravel, you’ll need to do the following:
1. Obtain a Google Captcha API key: You’ll need to obtain a Google Captcha API key from the Google Captcha site in order to use Captcha in your Laravel application. You can find instructions for obtaining an API key in the Google Captcha documentation.
2. Install the Google Captcha package: You can install the Google Captcha package for Laravel using Composer by running the following command:
composer require anhskohbo/no-captcha
3. Add the Captcha service provider: In your Laravel application’s config/app.php
configuration file, add the NoCaptchaServiceProvider
service provider to the providers
array:
'providers' => [
// ...
Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class,
],
4. Add the Captcha facade: In the same configuration file, add the NoCaptcha
facade to the aliases
array:
'aliases' => [
// ...
'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,
],
5. Configure the Captcha API key: In your Laravel application’s .env
file, add the following environment variables, replacing YOUR_SITE_KEY
and YOUR_SECRET_KEY
with your actual API keys:
NOCAPTCHA_SITEKEY=YOUR_SITE_KEY
NOCAPTCHA_SECRET=YOUR_SECRET_KEY
6. Use the Captcha in a form: Once you’ve installed and configured the Captcha package, you can use the NoCaptcha
facade to display a Captcha field in a form. Here’s an example of how you might use the Captcha in a form:
{!! NoCaptcha::renderJs() !!}
How to create custom validation for Google Captcha without using any package?
To write custom validation for Google Captcha in Laravel without using a package, you’ll need to do the following:
1. Verify the Captcha response: You’ll need to send the Captcha response to the Google Captcha API and verify that it is valid. You can do this by sending a POST request to the following URL:
https://www.google.com/recaptcha/api/siteverify
You’ll need to include the following parameters in the request:
secret
: Your Captcha secret key.response
: The Captcha response that was submitted by the user.remoteip
: The IP address of the user.
The API will return a JSON response indicating whether the Captcha was successfully verified.
2. Create a custom validation rule: In your Laravel application, you can create a custom validation rule by creating a class that implements the Illuminate\Contracts\Validation\Rule
interface. Here’s an example of a custom validation rule that verifies a Captcha response:
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use GuzzleHttp\Client;
class Captcha implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$client = new Client;
$response = $client->post('https://www.google.com/recaptcha/api/siteverify', [
'form_params' => [
'secret' => env('CAPTCHA_SECRET'),
'response' => $value,
'remoteip' => request()->ip(),
],
]);
$response = json_decode((string) $response->getBody());
return $response->success;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The Captcha verification failed. Please try again.';
}
}
3. Use the custom validation rule: Once you’ve created your custom validation rule, you can use your custom validation rule in a form request or a controller by adding the rule to the rules
array and using the validate
method.
For example, if you want to use the custom validation rule in a form request, you can do the following:
<?php
namespace App\Http\Requests;
use App\Rules\Captcha;
use Illuminate\Foundation\Http\FormRequest;
class ContactRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email',
'message' => 'required',
'captcha' => ['required', new Captcha],
];
}
}
4. Then, in your controller action, you can use the validate
method to validate the request:
public function store(ContactRequest $request)
{
// The request is valid, so you can process the form data...
}
If the Captcha validation fails, the user will be redirected back to the form with an error message.