Introduction to reCAPTCHA
reCAPTCHA is a new Google tool to protect your site against spammers and bots. It comes from a novice idea, because until then the captchas were seen more as an obstacle than a helper by the users. With reCAPTCHA this is different because all you have to do is click on a checkbox , confirming that it is not a robot. That way everyone wins, the user has faster access to the information they want, and you keep unwanted visitors to your site.
Incredible! I want to have one of these, how do I?
The first step is to get a key to your site. Go to the site below and click the blue button located at the top-right position of the site, typed "Get reCAPTCHA": link
Once you have registered your site, you will have two important information at hand: the site key , and the secret (I'll call it the secret key). The first step in implementing reCAPTCHA on your site is to insert the API into your site. Enter the code inside the head
tag of your site:
<script src='https://www.google.com/recaptcha/api.js'></script>
All ready for the next step.
Creating the form
Create a common form, I did with two fields: one for the user to put the name, and another for any message. The markup looks like this:
<h1>Exemplo de formulário com o reCaptcha</h1>
<form action="formulario.php" method="post">
Digite seu nome: <input type="text" name="nome"><br><br>
Deixe sua mensagem: <br>
<textarea name="mensagem" rows="7" cols="35"></textarea><br><br>
<input type="submit" value="Submit" name="formulario">
</form>
Inserting reCAPTCHA into the form is extremely simple, you just need to add the following element in the position you want it to appear:
<div class="g-recaptcha" data-sitekey="SUA-CHAVE"></div>
Do not forget to replace YOUR KEY with the key you received on the site, remembering that it is the site key , not the secret key >!
Replace the action
property in the form
tag with the name of the PHP file that will validate the form. You can validate on the same page, but I preferred to leave it in a separate one so the code gets simpler.
But can not I validate using JavaScript at the time?
No :) You'll understand why.
Creating the PHP code to validate the form
Let's get the value of the name and message field that the user sent:
if (isset($_POST['nome'])) {
$nome = $_POST['nome'];
}
if (isset($_POST['mensagem'])) {
$mensagem = $_POST['mensagem'];
}
From here the validation of captcha actually happens. We are sent via POST
a value provided by reCAPTCHA, it is possible to retrieve this value by the variable $_POST['g-recaptcha-response']
. Logo:
if (isset($_POST['g-recaptcha-response'])) {
$captcha_data = $_POST['g-recaptcha-response'];
}
// Se nenhum valor foi recebido, o usuário não realizou o captcha
if (!$captcha_data) {
echo "Por favor, confirme o captcha.";
exit;
}
It looks like it's over, right? Not! Now that's the fun part, remember when I said it was necessary that the captcha had to undergo a PHP validation? This is necessary because reCAPTCHA uses information in the Google database, which contains various information about the user who "made" the captcha. This way it is possible to distinguish an ordinary user from a bot. To validate the user, we need to make a request for the reCAPTCHA API, using file_get_contents
. Note:
$resposta = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SUA-CHAVE-SECRETA&response=".$captcha_data."&remoteip=".$_SERVER['REMOTE_ADDR']);
Look in the middle of the URL for YOUR-KEY-SECRET and replace it with yours. What this command does is to retrieve data in the reCAPTCHA API for information about the values that were provided by the captcha, in addition to sending the user's IP for future evaluations.
With the request sent, we only need to address the answer:
if ($resposta.success) {
echo "Obrigado por deixar sua mensagem!";
} else {
echo "Usuário mal intencionado detectado. A mensagem não foi enviada.";
exit;
}
I hope you have understood how the system works. Any questions you know, we're here to help. ;)