Fatal error: Class 'ReCaptcha\ReCaptcha'

Viewed 1568

I would really appreciate some help with implementing reCAPTCHA in my site. I created a simple one page site by altering a template i downloaded. The problem was my inbox was getting hammered by spam emails. I was told about reCAPTCHA and want to use it.

I have followed multiple examples online, searched on here and followed the google docs. I have come across various errors and managed to solve each one until now.

I have my own private and secret key and i have registered my actual domain.com and localhost.

The last tutorial i used was this;

https://bootstrapious.com/p/bootstrap-recaptcha

I downloaded the google API libraries into the root of my project with the following command;

git clone -b v1-master https://github.com/google/google-api-php-client.git.

As it stands i receive the following error.

Fatal error: Class 'ReCaptcha\ReCaptcha' not found in /Users/myName/Desktop/project/contactform/contactform.php on line 30.

I have uploaded my project to gitHub in the hope someone could help me out and take a look. I have removed any sensitive information from site but it will still run on your localhost.

Any help or suggestions will be greatly appreciated. Spent hours looking at it and trying different things.

https://github.com/Peakapot/reCAPTCHA-Help

Thank you

2 Answers

Try several days at my end, Google reCAPTCHA can be quite confusing with different solutions. Here's what worked for me:

<?php 

 if(isset($_POST['<data here>'])) { 
     $url = 'https://www.google.com/recaptcha/api/siteverify'; 
     $secretkey = "<secret key here>"; 

     $response = file_get_contents($url."?secret=".$secretkey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);         
     $data = json_decode($response); 

     if(isset($data->success) AND $data->success == true) { 

        // enter any code here 

     } else { 
         // False - display error  
         header('Location: /page.php?CaptchaFail=True'); // make sure it's on the same page 
     } 
 } 
 ?> 

Make sure the snippet above is placed at the top of the page above containing reCAPTCHA. Within HTML doc:

<?php if(isset($_GET['CaptchaFail'])){ ?> 
       <div>Captcha Failed. Please try again!</div> 
<?php } ?> 


<div class="g-recaptcha" data-sitekey="<site key here>"></div>                                     

The snippet above I used came from YouTube video that actually worked for me - https://www.youtube.com/watch?v=XjN0j4JQqVI&t=951s

Ok so after many many different methods of trying to solve this i finally have it working.

I found the answer here.

I have included a copy of the code that worked for me.

<?php
// require ReCaptcha class
require('../google-api-php-client/src/Google/autoload.php');
//GOOGLE RECAPTCH CODE
require_once('../recaptcha-master/src/ReCaptcha/ReCaptcha.php');
require_once('../recaptcha-master/src/ReCaptcha/RequestMethod.php');
require_once('../recaptcha-
master/src/ReCaptcha/RequestParameters.php');
require_once('../recaptcha-master/src/ReCaptcha/Response.php');
require_once('../recaptcha-
master/src/ReCaptcha/RequestMethod/Post.php');
require_once('../recaptcha-
master/src/ReCaptcha/RequestMethod/Socket.php');
require_once('../recaptcha-
master/src/ReCaptcha/RequestMethod/SocketPost.php');
$gRecaptchaResponse = $_POST['g-recaptcha-response'];
$secret = 'MY_KEY';

$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
if ($resp->isSuccess()) {

//DO ACTION IF SUCCESSFUL

if (isset($_POST['name']) && isset($_POST['email']) &&
        isset($_POST['subject']) && isset($_POST['message'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $outputMessage = $message ."\r\n\r\n" . $name;

    $headers = 'From: ' . $email . "\r\n" .
        'Reply-To: ' . $email . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail("mail@mail.com", $subject, $outputMessage, $headers);

   }



 } else {

//$errors = $resp->getErrorCodes();
echo "It appears you may be a robot. Please try again.";

} 

I realise that including all these files separately isn't good practice but its a work around i can live with for now.

Hope this helps someone.

Peakapot

Related