How can I integrate ReCaptcha into an HTML/PHP contact form?

Bracell

Well-known member
Joined
Jun 5, 2019
Messages
230
I have an outdated HTML website and I would like to incorporate ReCaptcha into the contact form. I am experiencing issues with a cryptocurrency spammer who is sending me spam messages through the contact form. Below is the contact form code from the index.php file (not the entire code, only the relevant section).


<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


The PHP file responsible for processing the contact form is named "contactprocess.php." I am unable to post the actual code here because of restrictions on Mmolearn, so I am sharing a link to CodePile, where the code can be found.

I have succeeded in integrating the ReCaptcha box by inserting specific code into the index.php file (which is not displayed in the above code, but I know the appropriate location to add the code). Here is the link to the code on CodePile https://www.codepile.net/pile/qnbObW5p


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

However, the ReCaptcha box must connect to the submit button in order to perform validation. I am unsure how to accomplish this, but I believe code must be included in the contactprocess.php file.

I have located two tutorials that may be beneficial in assisting you in aiding me:



If anyone is able to provide assistance, it would be greatly appreciated. Thank you.
 
Scorpion, why haven't you tried learning programming? It's quite simple! You'll find numerous helpful resources available.
 
Code:
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

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

and

substitute the section related to sending an email with this.

Code:
$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!';
}

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

Due to his laziness, he chooses to watch TV series rather than learning programming.
 
Code:
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

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

and

substitute the section related to sending an email with this.

Code:
$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!';
}

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

Due to his laziness, he chooses to watch TV series rather than learning programming.

Hey, friend, can you wait for a moment? I haven't had a meal in a considerable time, and suddenly, I started experiencing a sudden sensation of feeling hot/cold and dizzy. It came out of nowhere unexpectedly.

However, I'm familiar with this sensation; I'm sure it will disappear as soon as I eat something.

I'll be back in a minute...
 
Similar threads Most view View more
Back
Top Bottom