Add ReCaptcha to HTML/PHP contact form?

Scorpion Ghost

Elite Member
Executive VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
9,144
Reaction score
10,489
I have an old HTML website, and I'd like to add the ReCaptcha to the contact form, because some crypto spammer asshole keeps spamming me with his stupid trading bot or whatever.

This is the contact form:


index.php (it's not all the code, just what's relevant)

HTML:
  <div style="float: right;"><form action="contactprocess.php" method="post" name="freecontactform" id="freecontactform" onsubmit="return validate.check(this)">
    <table width="550" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td valign="top" style="font-weight:bold;">Your Name:</td>
      </tr>
      <tr>
        <td valign="top" class="formtd"><input name="Full_Name" type="text" class="formtxt" id="Full_Name" /></td>
      </tr>
      <tr>
        <td valign="top" style="font-weight:bold;">Your Email Address:</td>
      </tr>
      <tr>
        <td valign="top" class="formtd"><input name="Email_Address" type="text" class="formtxt" id="Email_Address" maxlength="100" /></td>
      </tr>
      <tr>
        <td valign="top" style="font-weight:bold;">Your Message:</td>
      </tr>
    
      <tr>
        <td valign="top" class="formtd"><textarea name="Your_Message" class="formtxt" id="Your_Message" style="height:160px" maxlength="2000"></textarea></td>
      </tr>
    
      <tr>
        <td><input name="AntiSpam" type="hidden" id="AntiSpam"  value="25" maxlength="100" />
          <div class="checkout"><a href="#" onclick="document.forms['freecontactform'].submit();">Send Message</a></div></td>
      </tr>
    
    </table>
    <p>&nbsp;</p>
  </form></div>


contactprocess .php

https://www.codepile.net/pile/JnKJ4weR (im posting this link where you can find the code, because Blackhatworld is being a dildo and won't let me post the actual code here)


I managed to add the ReCaptcha box by adding these in index.php (it's not shown in the code above, but I know how/where to add these):

https://www.codepile.net/pile/qnbObW5p (sharing this link because BHW dildo)

HTML:
<div class="g-recaptcha brochure__form__captcha" data-sitekey="YOUR SITE KEY"></div>


But the ReCaptcha box needs to somehow connect to the submit button, to do the validation. I don't know how to do that, but I'm pretty sure code needs to be added to contactprocess.php


Here are 2 tutorials that may be of help to help you help me kinda thing:

https://www.bronco.co.uk/our-ideas/how-to-add-google-recaptcha-to-a-form-phphtml/
https://yourblogcoach.com/how-to-add-google-recaptcha-in-php-contact-form/

If anyone can help, would be great. Thanks : )
 
Last edited:
Add

PHP:
function reCaptcha($recaptcha){
  $secret = "YOUR SECRET KEY";
  $ip = $_SERVER['REMOTE_ADDR'];

  $postvars = array("secret"=>$secret, "response"=>$recaptcha, "remoteip"=>$ip);
  $url = "https://www.google.com/recaptcha/api/siteverify";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
  $data = curl_exec($ch);
  curl_close($ch);

  return json_decode($data, true);
}

before

PHP:
$email = $_POST['Email_Address'];

and

replace the part with sending email with this

PHP:
$recaptcha = $_POST['g-recaptcha-response'];
$res = reCaptcha($recaptcha);
if($res['success']){
  if(@mail($your_email,$email_subject,$email_content)) {

        header('Location: http://site.com');

    }

   

    if(@mail($email,$email_subjectreply,$email_autoreply,$header = 'From: site.com <[email protected]>' . "\r\n")) {

        header('Location: http://site.com');

    }
}else{
  echo 'ERROR!';
}

Why you don't learn programming Scorpion? It's easy! You can use a lot of blue tack there.

Because he is lazy, watching TV series instead of learning programming.
 
Add

PHP:
function reCaptcha($recaptcha){
  $secret = "YOUR SECRET KEY";
  $ip = $_SERVER['REMOTE_ADDR'];

  $postvars = array("secret"=>$secret, "response"=>$recaptcha, "remoteip"=>$ip);
  $url = "https://www.google.com/recaptcha/api/siteverify";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
  $data = curl_exec($ch);
  curl_close($ch);

  return json_decode($data, true);
}

before

PHP:
$email = $_POST['Email_Address'];

and

replace the part with sending email with this

PHP:
$recaptcha = $_POST['g-recaptcha-response'];
$res = reCaptcha($recaptcha);
if($res['success']){
  if(@mail($your_email,$email_subject,$email_content)) {

        header('Location: http://site.com');

    }

  

    if(@mail($email,$email_subjectreply,$email_autoreply,$header = 'From: site.com <[email protected]>' . "\r\n")) {

        header('Location: http://site.com');

    }
}else{
  echo 'ERROR!';
}



Because he is lazy, watching TV series instead of learning programming.

Bro give me a minute. I haven't eaten for so long and just now I got like hot/cold and dizzy feeling all of a sudden. Just hit me out of the blue.

But I know this feeling, the second I eat it will go away.

Back in a minute...
 
Why you don't learn programming Scorpion? It's easy! You can use a lot of blue tack there.

haha haha :D

It's not easy. You know what's easy-ish? CSS is easy-ish. You can wrap your head around some CSS for the most part.

Even HTML, like, okay.

The rest of it, OMG :o

Add

PHP:
function reCaptcha($recaptcha){
  $secret = "YOUR SECRET KEY";
  $ip = $_SERVER['REMOTE_ADDR'];

  $postvars = array("secret"=>$secret, "response"=>$recaptcha, "remoteip"=>$ip);
  $url = "https://www.google.com/recaptcha/api/siteverify";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
  $data = curl_exec($ch);
  curl_close($ch);

  return json_decode($data, true);
}

before

PHP:
$email = $_POST['Email_Address'];

and

replace the part with sending email with this

PHP:
$recaptcha = $_POST['g-recaptcha-response'];
$res = reCaptcha($recaptcha);
if($res['success']){
  if(@mail($your_email,$email_subject,$email_content)) {

        header('Location: http://site.com');

    }

  

    if(@mail($email,$email_subjectreply,$email_autoreply,$header = 'From: site.com <[email protected]>' . "\r\n")) {

        header('Location: http://site.com');

    }
}else{
  echo 'ERROR!';
}



Because he is lazy, watching TV series instead of learning programming.

Damn man, I just ate, felt better, and crashed. Slept until now.

And in all fairness, I mostly "listen" to the shows, I don't actually watch watch. But I did watch watch ZeroZeroZero for the most part, it was great show :D

---

Okay, regarding the catpcha. I added everything, checked everything, but it still lets the message through even if the captcha is not completed :/
 
Oh no, actually it works. It wouldn't work at first, but now it works.

Oh yeeeee :cool:

Any idea how to add a message to the captcha error?

If there is no input in "email" field the error is -> Invalid email address entered!

If the input is less than 15chars in message box the error is -> Message too short!

If captcha is not solved the error is -> ERROR!

I'd like to add something a little more descriptive...
 
Oh no, actually it works. It wouldn't work at first, but now it works.

Oh yeeeee :cool:

Any idea how to add a message to the captcha error?

If there is no input in "email" field the error is -> Invalid email address entered!

If the input is less than 15chars in message box the error is -> Message too short!

If captcha is not solved the error is -> ERROR!

I'd like to add something a little more descriptive...

Okay, never mind, I figured it out (who says I can't code? :D )

Thanks bro @TomTheCat you've saved me hours of my life receiving/opening/deleting spam messages. You're the best! :)

:D

 
Last edited:
Bro give me a minute. I haven't eaten for so long and just now I got like hot/cold and dizzy feeling all of a sudden. Just hit me out of the blue.

But I know this feeling, the second I eat it will go away.

Back in a minute...
If you want to find courage to learn programming, you must eat 5 times a day. Otherwise, you will have to ask questions. :)
haha haha :D

It's not easy. You know what's easy-ish? CSS is easy-ish. You can wrap your head around some CSS for the most part.

Even HTML, like, okay.

The rest of it, OMG :eek:
Watch some tutorials on YouTube. Code a real world app. You will grasp it that way. There is a lot of common patterns you will start to notice as you learn. You will have to watch less tv shows and post shorter replies on bhw. That's a lot of changes.
 
If you want to find courage to learn programming, you must eat 5 times a day. Otherwise, you will have to ask questions. :)
Watch some tutorials on YouTube. Code a real world app. You will grasp it that way. There is a lot of common patterns you will start to notice as you learn. You will have to watch less tv shows and post shorter replies on bhw. That's a lot of changes.

Too much school :weep:

 
@TomTheCat bro need you to come to the rescue once again.

So I'm making my new site. I added recaptcha on the blog post comments sections with this - https://pluginsforwp.com/blog/add-recaptcha-to-wordpress-comments/

I added recaptcha on the Contact Us page with this - https://github.com/bozdoz/wp-plugin-recaptcha-jetpack

And now for last I want to add it to the Register page. I tried a plugin which was supposed to give a shortcode, but the shortcode was for paid version. Plus I'm pretty sure that wouldn't work, because it's not that simple. Plus I prefer to not use plugins unless I have to...

So I'm trying the same method as I tried in this thread in the opening post, but I'm failing. So figured out where to put the code in order for the captcha to show, and then I tried putting this in functions.php down at the bottom:

PHP:
function reCaptcha($recaptcha){
  $secret = "YOUR SECRET KEY";
  $ip = $_SERVER['REMOTE_ADDR'];

  $postvars = array("secret"=>$secret, "response"=>$recaptcha, "remoteip"=>$ip);
  $url = "https://www.google.com/recaptcha/api/siteverify";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
  $data = curl_exec($ch);
  curl_close($ch);

  return json_decode($data, true);
}

The captcha appears where I want it, but it lets a person register even if not submitted.

The registration page on this site isn't a page per se, it's like a popup that you can open on any page on the site.

Any ideas?
 
Last edited:
So I'm making my new site. I added recaptcha on the blog post comments sections with this - https://pluginsforwp.com/blog/add-recaptcha-to-wordpress-comments/

I added recaptcha on the Contact Us page with this - https://github.com/bozdoz/wp-plugin-recaptcha-jetpack

So just to say something about these.

The second plugin, the one from the Github link, when a user doesn't submit the captcha successfully, they get the error right there on the page.

Here it is as a plugin - https://wordpress.org/plugins/recaptcha-jetpack/

Last updated: 5 years ago <- Let that sink in...

Like this (i edited it a bit for color and margin):

ggggg.PNG


And this plugin is like a million years old, hasn't been updated in forever.

---

But the other method that I used to add the recaptcha to the blog posts comment section, if it's not submitted successfully, you get take to an entirely different page to see the error:

987987.PNG


I also tried 2 plugins that are widely recommended for adding Recaptcha to wordpress, and they both behave the same way:

Last updated: 6 months ago - https://wordpress.org/plugins/simple-google-recaptcha/Last updated: 4 months ago - https://wordpress.org/plugins/google-captcha/
What did we learn here today?

1) This is ridiculous
2) They don't make em like they used to

God protect me from updates and updated software :p

---

I'll give it a few more minutes, but I'll probably have to settle for the error going on a new page. It's not a big deal, but it's not perfect...
 
Uhm, maybe you need to find an action hook for the registration form or do some filtering with a filter hook. You can always adjust the code...

jk

That glue + paper tape style of coding is terrifying. It's a set of practices any reasonable programmer would be yelling for weeks about. They would remove the whole project and do it all over again, migrating data.

It looks like blue tack of coding.
 
Uhm, maybe you need to find an action hook for the registration form or do some filtering with a filter hook. You can always adjust the code...

jk

That glue + paper tape style of coding is terrifying. It's a set of practices any reasonable programmer would be yelling for weeks about. They would remove the whole project and do it all over again, migrating data.

It looks like blue tack of coding.

I am a blue tack coder xD

So I also tried this - https://wordpress.org/plugins/advanced-google-recaptcha/

Same thing, error on a new page...
 
I am a blue tack coder xD

So I also tried this - https://wordpress.org/plugins/advanced-google-recaptcha/

Same thing, error on a new page...
Yes, you are. Imagine the time spent on this could be spent on learning programming syntax and concepts while applying them in practice. After a few trials you would understand what the logic behind your scripts is.

I was doing blue tack coding for about 2 years when I was 16, 17, 18. Then I said "fck this, it's so hard to do this". And I learned it.
 
Yes, you are. Imagine the time spent on this could be spent on learning programming syntax and concepts while applying them in practice. After a few trials you would understand what the logic behind your scripts is.

I was doing blue tack coding for about 2 years when I was 16, 17, 18. Then I said "fck this, it's so hard to do this". And I learned it.

Yeah, but once I'm done I'm done. And I get 95% of what I need done perfectly. The other 4% I get some BHW help. And the other 1% I settle for what I can get :p

I also tried this - https://wordpress.org/plugins/wp-recaptcha-integration/

It has an interesting option - "Disable Form Submit Button until no-captcha is entered."

I thought that I had found my solution. But unfortunately, the button stays disabled even after captcha is entered :oops:
 
Tried this too - https://wordpress.org/plugins/wprecaptcha/

Doesn't work. Comments just don't get submitted, no matter solved captcha or not.

It's unfortunate because it has a shortcode option, which I was gonna try on the Registration form.

But it doesn't work...
 
Also tried this one, and it doesn't work. Just you can't click submit no matter what.

https://wordpress.org/plugins/recaptcha-in-wp-comments-form/
Okay, I gave it a shot, nothing works. I'll go back to how I had it initially and call it a day.

The blog on this website is more like a filler thing than an actual blog I care about, and it doesn't get many comments anyway. So if someone once in a blue moon wants to post a comment, and they forget to do the captcha or whatever, they'll have to see the error page and then click to go back and try again.

At least, from what I see, if you get to the error page and then click to go back, your content is saved. The Message, Name, Email, Website, all of that is saved, so you don't have to get pissed off that you have to fill in all that info again. I mean, I don't know this is dependent on browser cache or whatever, but I don't care, whatever.

---

I still need to figure out how to add recaptcha on the Registration form...
 
You have to put that function in the file
PHP:
function reCaptcha($recaptcha){
  $secret = "YOUR SECRET KEY";
  $ip = $_SERVER['REMOTE_ADDR'];

  $postvars = array("secret"=>$secret, "response"=>$recaptcha, "remoteip"=>$ip);
  $url = "https://www.google.com/recaptcha/api/siteverify";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
  $data = curl_exec($ch);
  curl_close($ch);

  return json_decode($data, true);
}

This code is already here https://github.com/bozdoz/wp-plugin-recaptcha-jetpack/blob/master/recaptcha-jetpack.php#L143-L190

You don't have to add it. You know I'm not a WP fan. The IP part is missing, maybe that's causing the issue?

Change
PHP:
$querystring = sprintf('secret=%s&response=%s', $secret_key, $recaptcha_reponse);

with
PHP:
$querystring = sprintf('secret=%s&response=%s&remoteip=%s', $secret_key, $recaptcha_reponse, $_SERVER['REMOTE_ADDR']);
 
Back
Top