Contact Form with reCaptcha

4

I have a contact form and I want to implement reCaptcha but the truth is that I have not been able to, I have tried but I can not get my PHP code to validate that the Captcha is not resolved and does not allow sending the form. Then my PHP and HTML code used.

In <head></head> I have put as the official website says:

<script src='https://www.google.com/recaptcha/api.js'></script>

HTML:

    <form action="enviar.php" method="post" enctype="multipart/form-data">
    <h1 class="title">Contáctanos</h1>
    <input name="name" required="required" placeholder="Nombre">
    <br>
    <input name="email" type="email" required="required" placeholder="Email">
    <br>
    <select name="enquirytype">
        <option value="1">Publicar un Libro</option>
        <option value="2">Error con Algún libro</option>
        <option value="3">Otro error</option>
    </select>
    <br>
    <textarea name="message" cols="20″ rows="5″ required="required" placeholder="Mensaje"></textarea>
    <br>
   <div class="g-recaptcha" data-sitekey="MiClaveAquí"></div>
    <br>
    <input id="submit" name="submit" type="submit" value="Submit">
    </form>

PHP (send.php):

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: YourWebsite.com'; 
    $to = '[email protected]'; 
    $subject = 'Soporte Técnico - RelaxMind';
    $ip = $_SERVER['REMOTE_ADDR'];
    $select_enquirytype = strip_tags($_POST['enquirytype']);

    $body = "From: $name\n E-Mail: $email\n Message:\n $message \n Option $select_enquirytype \n IP User: $ip";
?>

<?php
if ($_POST['submit']) {
    if (mail ($to, $subject, $body, $from)) { 
        echo '<p>Gracias, nos comunicaremos lo mas pronto posible!</p>';
    } else { 
        echo '<p>Oh no! Hubo un error, Intenta mas tarde :(</p>'; 
    }
}
?>

Saying that I have used it as it is in the ReCaptcha Help appears, only that I can not get PHP to validate that the Captcha was or was not resolved. That would be all my code, could you help me out? Thanks in advance.

    
asked by Joseph Gonzaga 30.05.2017 в 23:27
source

2 answers

2

The client part (the html) is correct, but the server part is missing where it checks if the recaptcha has been passed.

In the enviar.php you should add something like this:

$captcha =  isset( $_POST['g-recaptcha-response'] ) ? $_POST['g-recaptcha-response'] : '';
if (!$captcha) {
   die('Por favor resuelva el captcha');
}

$secret = 'tu_clave_secreta_de_recaptcha';
$response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);

if ($response['success'] === true) {
   // Aqui envías el correo del formulario etc
} else {
   die('Captcha incorrecto, actualice la página pulsando F5');
}
    
answered by 07.06.2017 / 14:20
source
1
$response = null;
// comprueba la clave secreta
$reCaptcha = new ReCaptcha($secret);
if ($_POST["g-recaptcha-response"]) {
 $response = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
}
if ($response != null && $response->success) {

// Si el código es correcto, seguimos procesando el formulario como siempre
}else{
//Mensaje para avisar que debe completar el captcha
}

This goes in your send.php code.

    
answered by 09.10.2017 в 18:08