How to implement google recaptcha in my form

Hi
I am currently working in a project . My webpage has a form.
Now I want to implement the google recapture on this form.
please have a look
/payingsitesranks.hits2sites.com

I have created two php file

  1. new.php ( this file has the form)
  2. verify.php ( to verify the submited result)

Here is the files

new.php

[php]<?php
include(‘config.php’);
?>

Add a Website to the Top site
Top site
<?php //We check if the form has been submitted if(isset($_POST['confirmation']) and $_POST['confirmation']=='true') { //We remove slashes depending of the configuration if(get_magic_quotes_gpc()) { $_POST['name'] = stripslashes($_POST['name']); $_POST['url'] = stripslashes($_POST['url']); $_POST['description'] = stripslashes($_POST['description']); $_POST['banner'] = stripslashes($_POST['banner']); $_POST['email'] = stripslashes($_POST['email']); } //We get the futur ID of the Website $req1 = mysql_fetch_array(mysql_query('select max(id) as id from topsite')); $id = intval($req1['id'])+1; //We send an email to the webmaster if($mail) { $subject = 'Your website subscription - Top site'; $message = "Hy, You recieve this message after the subscription of your website(".$_POST['url'].") on our top site. An Admin will check it soon and decide to accept it or not. You can start earning votes by sharing the following link on your website:

<a href="".$url_root.“vote.php?id=”.$id."">Vote for this Website

Thank you";
$to = $_POST[‘email’];
$headers = “From: “Top site”<”.$mail_webmaster.">\n";
$headers .= “Reply-To: “.$mail_webmaster.”\n”;
$headers .= “Content-Type: text/plain; charset=“utf-8"”;
mail($to,$subject,$message,$headers);
}
//We protect the variables and we limit the number of letters of some fields
$name = mysql_escape_string(substr($_POST[‘name’],0,55));
$url = mysql_escape_string($_POST[‘url’]);
$description = mysql_escape_string(substr($_POST[‘description’],0,255));
$banner = mysql_escape_string($_POST[‘banner’]);
$email = mysql_escape_string($_POST[‘email’]);
//On ajoute le site a la bdd
if(mysql_query('insert into topsite (id, name, url, description, banner, email, votes, status) values (”’.$id.’", “’.$name.’”, “’.$url.’”, “’.$description.’”, “’.$banner.’”, “’.$email.’”, 0, “no”)’))
{
echo ‘Your website has successfully been submited, an admin will check it soon.

You can start earning votes:

<input type=“text” value="’.htmlentities(‘Vote for this Website’).’" size=“50” onclick=“this.select();” />’;
}
else
{
echo ‘An error occurred while submitting the website.’;
}
}
else
{
//We display the form
?>
To add your website to this top site, you have to fill the following form:

Website Name
URL
Description
Banner
Email
<?php } ?>
Return to the Top site - Webestools
[/php]

verify.php code

[php]<?php
include(‘config.php’);
?>

Add a Website to the Top site
Top site
<?php //We check if the form has been submitted if(isset($_POST['confirmation']) and $_POST['confirmation']=='true') { //We remove slashes depending of the configuration if(get_magic_quotes_gpc()) { $_POST['name'] = stripslashes($_POST['name']); $_POST['url'] = stripslashes($_POST['url']); $_POST['description'] = stripslashes($_POST['description']); $_POST['banner'] = stripslashes($_POST['banner']); $_POST['email'] = stripslashes($_POST['email']); } //We get the futur ID of the Website $req1 = mysql_fetch_array(mysql_query('select max(id) as id from topsite')); $id = intval($req1['id'])+1; //We send an email to the webmaster if($mail) { $subject = 'Your website subscription - Top site'; $message = "Hy, You recieve this message after the subscription of your website(".$_POST['url'].") on our top site. An Admin will check it soon and decide to accept it or not. You can start earning votes by sharing the following link on your website:

<a href="".$url_root.“vote.php?id=”.$id."">Vote for this Website

Thank you";
$to = $_POST[‘email’];
$headers = “From: “Top site”<”.$mail_webmaster.">\n";
$headers .= “Reply-To: “.$mail_webmaster.”\n”;
$headers .= “Content-Type: text/plain; charset=“utf-8"”;
mail($to,$subject,$message,$headers);
}
//We protect the variables and we limit the number of letters of some fields
$name = mysql_escape_string(substr($_POST[‘name’],0,55));
$url = mysql_escape_string($_POST[‘url’]);
$description = mysql_escape_string(substr($_POST[‘description’],0,255));
$banner = mysql_escape_string($_POST[‘banner’]);
$email = mysql_escape_string($_POST[‘email’]);
//On ajoute le site a la bdd
if(mysql_query('insert into topsite (id, name, url, description, banner, email, votes, status) values (”’.$id.’", “’.$name.’”, “’.$url.’”, “’.$description.’”, “’.$banner.’”, “’.$email.’”, 0, “no”)’))
{
echo ‘Your website has successfully been submited, an admin will check it soon.

You can start earning votes:

<input type=“text” value="’.htmlentities(‘Vote for this Website’).’" size=“50” onclick=“this.select();” />’;
}
else
{
echo ‘An error occurred while submitting the website.’;
}
}
else
{
//We display the form
?>
To add your website to this top site, you have to fill the following form:

Website Name
URL
Description
Banner
Email
<?php } ?>
Return to the Top site - Webestools
[/php]

I have already got the private key and site key from recaptcha site

Please implement the recaptcha verification on verify.php file

You’re in luck, for I just did google’s recaptcha v2 for my website.

I’ll show it to you for I find it easier that way -> first the captcha itself:
My contact page (contact.php)
[php]

Pepster's Place - Circle of Life
Contact Form First Name Last Name Email Address
Leave blank
comment pricing order info
        <label class="textareaLabel" for="message">Message</label>
        <textarea id="message" name="comment" placeholder="Enter message here..."></textarea>
        <!-- This is where you put your public key at (inside the form) -->
                    <div class="g-recaptcha" data-sitekey="your_public_key_goes_here"></div>
        <input type="submit" name="submit" value="submit">
    </fieldset>
    <!-- Continuation of non-essential  HTML --> 

[/php]

The place where I process the contact submission
[php]$submit = filter_input(INPUT_POST, ‘submit’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

if (isset($submit) && $submit === ‘submit’) {
$url = “https://www.google.com/recaptcha/api/siteverify”;
/* PRIVATE_KEY is a constant I use that I put in my connection page outside of this page */
define(‘PRIVATE_KEY’, ‘your_private_key_goes_here’);

$remoteServer = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_URL);
$response = file_get_contents($url . "?secret=" . PRIVATE_KEY . "&response=" . \htmlspecialchars($_POST['g-recaptcha-response']) . "&remoteip=" . $remoteServer);
$recaptcha_data = json_decode($response);

if (isset($recaptcha_data->success) && $recaptcha_data->success === TRUE) {
   /* This is where you will process if there is human interaction */
} else {
    $error = "You're not a human!";
}

}[/php]

I was unable to test it locally other than getting the css appearance if I put localhost as one of the approved servers, but testing it remotely isn’t that big of a deal (at least for me it wasn’t). HTH John

BTW - I just looked at your script. Do yourself a favor and try to use a more up-to-date script for magic quotes went the way the Dodo bird went. :o

Sponsor our Newsletter | Privacy Policy | Terms of Service