Skip to content

Instantly share code, notes, and snippets.

@Crowles
Last active November 10, 2017 14:10
Show Gist options
  • Save Crowles/7f18264d82c3efaf58f14494091ba442 to your computer and use it in GitHub Desktop.
Save Crowles/7f18264d82c3efaf58f14494091ba442 to your computer and use it in GitHub Desktop.
Google's reCAPTCHA trait class - use with google/recaptcha package.
<?php
/**
* Reusable Captcha trait.
*
* to use this trait, simply include it inside your class via use statement.
* Then you just need to call $this->captchaCheck();
*/
namespace App\Http\Traits;
use Illuminate\Support\Facades\Input;
use ReCaptcha\ReCaptcha;
/**
* Trait CaptchaTrait
*/
trait CaptchaTrait {
public function captchaCheck()
{
$response = Input::get('g-recaptcha-response');
$remoteip = $_SERVER['REMOTE_ADDR'];
$secret = env('RECAPTCHA_SECRET_KEY');
$recaptcha = new ReCaptcha($secret);
$resp = $recaptcha->verify($response, $remoteip);
if ($resp->isSuccess()) {
return 1;
} else {
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment