Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

Recaptcha

Hello,

I am trying to utilize this recatcha:

https://developers.google.com/recaptcha/docs/v3

but do not know how to?

This is what I did so far:

	<script src="https://www.google.com/recaptcha/api.js"></script>

	....
	<div class="form-group">
     <label class="control-label col-sm-2" for="pwd">Pesan:</label>
     <div class="col-sm-10">
       <textarea id="pesan" name="pesan" class="form-control"></textarea>
     </div>
   </div>

   <button class="g-recaptcha" 
    data-sitekey="reCAPTCHA_site_key" 
    data-callback='onSubmit' 
    data-action='submit'>Submit</button>


   <div class="form-group">
     <div class="col-sm-offset-2 col-sm-10">
       <button type="submit" class="btn btn-default">Submit</button>
     </div>
   </div>
  </form>

	<script>
function onSubmit(token) {
 		document.getElementById("pesan").submit();
}
	</script>

I also do not know where to get: reCAPTCHA_site_key if there is any?

0 likes
12 replies
Akash_kushwaha's avatar

I also do not know where to get: reCAPTCHA_site_key if there is any?

add reCAPTCHA_site_key in your .env file and call it like this

{{ env('reCAPTCHA_site_key') }}
davy_yg's avatar
Level 27

Here I found google recaptcha: https://packagist.org/packages/google/recaptcha

It gives me this for the backend:

		<?php

		require_once '/path/to/recaptcha/src/autoload.php';

		$recaptcha = new \ReCaptcha\ReCaptcha($secret);
		$resp = $recaptcha->setExpectedHostname('recaptcha-demo.appspot.com')
              ->setExpectedAction('homepage')
              ->setScoreThreshold(0.5)
              ->verify($gRecaptchaResponse, $remoteIp);

		if ($resp->isSuccess()) {
				// Verified!
				return view('store.contact', compact('recaptcha'));
		} else {
			$errors = $resp->getErrorCodes();
		}

Yet, I do not know how to show the recaptcha on the frontend. Is it like this?

		{{ $recaptcha }}

I actually get this:

Undefined variable: recaptcha (View: F:\xampp74\htdocs\zws_admin_8\resources\views\contact.blade.php)

davy_yg's avatar
Level 27

Thanks. I am trying to implement the package:

Error Class 'Illuminate\Support\Facades\Input' not found

FPController.php

		use Illuminate\Support\Facades\Input;
		use Arcanedev\NoCaptcha\Rules\CaptchaRule;
		
		...
		public function sendmail(request $request)
		{

    	$blogs = Blog::take(3)->get();
    	$contact = Contact::find(1);
    	$setting = Setting::first();

    	$this->recaptcha($contact);

    	return view('contact')->with(compact('blogs', 'contact', 'setting'));
	}

	public function recaptcha($contact)
	{

    $inputs   = Input::all();
    $rules    = [
                // Other validation rules...
                'g-recaptcha-response' => ['required', new CaptchaRule],
                ];

    $messages = [
                'g-recaptcha-response.required' => 'Your custom validation message.',
                'g-recaptcha-response.captcha'  => 'Your custom validation message.',
                ];

    $validator = Validator::make($inputs, $rules, $messages);

    if ($validator->fails()) {
                $errors = $validator->messages();

                var_dump($errors->first('g-recaptcha-response'));

                // Redirect back or throw an error

                Session::flash('error', 'Error recaptcha!');
                }
        else {

                Mail::to($contact->contact_email)
                    ->send(new ContactMail($request->all()));

                Session::flash('flash', 'successfully send email');

                }

		}

config/app.php

			'aliases' => [
				
			...
		     'Input' => Illuminate\Support\Facades\Input::class,
		    ],

ref: https://stackoverflow.com/questions/31696679/laravel-5-class-input-not-found

Snapey's avatar

why are you mixing up sending the email with the recaptcha?

just use it as a validation rule when you validate the other fields

davy_yg's avatar
Level 27

@Snapey I thought if the recaptcha validation did not failed, then I thought proceed the contact form (send the content of the contact form to me).

This code also causing error: contact.blade.php

		{!! no_captcha()->display() !!}

Call to undefined method Arcanedev\NoCaptcha\NoCaptchaV3::display() (View: F:\xampp74\htdocs\zws_admin_8\resources\views\contact.blade.php)

ref: https://github.com/ARCANEDEV/noCAPTCHA/issues/90

Please or to participate in this conversation.