Recaptcha仅在我点击它们时才有效

时间:2018-03-26 12:08:04

标签: recaptcha

我在这里有一个代码,但是如果我点击 reCaptcha ,他会确认,这没问题。

但是如果我不点击确认 reCaptcha 并且只提交 表格,reCaptcha不起作用,让我确认表格。

HTML

<?php
      if ($response != null && $response->success) {
        echo "Olá, " . $_POST["name"] . " (" . $_POST["email"] . "), obrigado por enviar seu formulário!";
      } else {

?>

<div class="container"></div>

<section class="container section">
    <div class="row">
        <div class="col-sm-6">
            <div class="successMessage alert alert-success" style="display: none">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                Obrigado.
            </div>
            <div class="errorMessage alert alert-danger" style="display: none">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                Ops! Algo de errado aconteceu.
            </div>

            <form role="form" action="assets/php/enviaEmail.php" method="post" class="contactForm validateIt" data-show-errors="true">
                <div class="row padding-xs-top">
                    <div class="col-md-6 col-sm-6">
                        <div class="form-group form-group-float-label">
                            <input id="contact_name" required type="text" name="contact_name" class="form-control input-lg">
                            <label for="contact_name">Nome *</label>
                        </div>
                    </div>
                <div class="row">
                    <div class="col-xs-12 col-sm-12 col-md-12">
                        <div class="form-group form-group-float-label">
                            <textarea id="contact_message" class="form-control input-lg" rows="4" name="contact_message" required ></textarea>
                            <label for="contact_message">Mensagem *</label>
                        </div>
                    </div>
                </div>

                <div class="g-recaptcha" data-theme="dark" data-sitekey=""></div>

                <div class="row">
                    <div class="col-xs-12">
                        <button type="submit" style="float:left; margin-top:3% !important;" class="btn btn-primary btn-lg pull-right">Enviar</button>
                    </div>
                </div>
            </form>
        </div>

<?php } ?>

PHP

if($_POST){

// se submetido, verifique a resposta
if (isset($_POST["g-recaptcha-response"]) && $_POST["g-recaptcha-response"]) { //isset informa se a variavel foi iniciada
      var_dump($_POST); //mostra informação sobre a variavel
      $secret = "";
      $ip = $_SERVER['REMOTE_ADDR'];
      $captcha = $_POST['g-recaptcha-response'];
      $rsp = file_get_contents ("https://www.google.com/recaptcha/api/siteverify?secret=$secret&captcha=$captcha&remoteip$ip"); //Lê todo o conteudo de um arquivo para uma string
      var_dump($rsp); //mostra informacao sobre a variavel
      $arr = json_decode($rsp,TRUE); //decodifica uma string

      if($arr['success'){ 
        echo '<script> alert("Done")</script>;';

      } else {

        echo '<script> alert("erro"); </script>';
      }

    $response = $reCaptcha->verifyResponse(
            $_SERVER["REMOTE_ADDR"],
            $_POST["g-recaptcha-response"]
        );

    }

0 个答案:

没有答案