0
I made a form in PHP with Captcha do Google. Everything works. If someone sends the email via form, it arrives perfect, has required, etc. The problem is that spans access the email page on the site, example "www.site.com.br/send-email.php".
You can put a code inside the same file preventing them from accessing this page or preventing the code from working because the fields are empty?
That is the code:
<?
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => '____aqui a secret key____',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);
if (!$res['success']) {
// What happens when the CAPTCHA wasn't checked
echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
} else {
// If CAPTCHA is successfully completed...
// Paste mail function or whatever else you want to happen here!
echo '<br><p>CAPTCHA was completed successfully!</p><br>';
}
$nome = $_POST['nome'];
$fone = $_POST['telefone'];
$email = $_POST['endereco'];
$assunto = $_POST['assunto'];
$msg = $_POST['msg'];
$conteudo = "<table width='600' border='0' cellspacing='2' cellpadding='2'>
<tr>
<td colspan='1' align='center'><h3><em>Assunto do E-mail</em></h3></td>
</tr>
<tr>
<td width='30%' bgcolor='#f0f0f0'><strong>Nome:</strong></td>
<td>$nome</td>
</tr>
<tr>
<td width='30%' bgcolor='#f0f0f0'><strong>Telefone:</strong></td>
<td>$fone</td>
</tr>
<tr>
<td width='30%' bgcolor='#f0f0f0'><strong>Email:</strong></td>
<td>$email</td>
</tr>
<tr>
<td width='30%' bgcolor='#f0f0f0'><strong>Assunto:</strong></td>
<td>$assunto</td>
</tr>
<tr>
<td width='30%' bgcolor='#f0f0f0'><strong>Mensagem:</strong></td>
<td>$msg</td>
</tr>
</table>";
$seuemail = "[email protected]";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From:".$email." \r\n";
$assunto = $assunto;
$enviar = mail($seuemail, $assunto, $conteudo, $headers);
if($enviar) {
echo "<script type='text/javascript'> alert('Contato Enviado com Sucesso!'); window.location.href='contato.php'; </script>";
}else{
echo "<script type='text/javascript'> alert('Ocorreu algum erro ao enviar o formulário'); </script>";
}
?>
How do you validate Google’s Captcha when sending the email? In the code posted it does not seem to have this check.
– Woss
Hello @Andersoncarloswoss, updated the code! Before the <button> I insert the <div class="g-recaptcha"> and the <script src='https://www.google.com/recaptcha/api.js?hl=pt-BR'></script> But for the tests I am doing with the Lipespry response I removed the captcha.
– Angel Azevedo
But this way the email will be sent even if the captcha is invalid, because the sending function is outside the
if/else
. Or you add onedie(...)
within theif
to stop the execution, or place the rest of the code, which makes the upload, inside theelse
, as the comment itself indicates:Paste mail function or whatever else you want to happen here!
.– Woss
Wow... Simpler than I imagined @Andersoncarloswoss, lack of reading about the code, even being layman in PHP. Thank you so much, that way when the sending page is accessed directly by spans, it says to go back and perform the Captcha.
– Angel Azevedo
Exactly. The solution given by Lipespry in the answers is interesting, but not completely functional, because it only deals with whether the data is null. If a spam system requests your file with non-null data, the email would be sent as well. Checking the captcha, as said, circumvents this problem.
– Woss
Got it! I also found interesting the answer given by him, some time ago wanted to understand how it works. Anyway, thank you both!
– Angel Azevedo