如何强制或要求Google reCaptcha?

时间:2018-04-06 00:04:08

标签: javascript php jquery html recaptcha

我有一个联系表格,我已成功添加了reCAPTCHA v2,但我无法将其作为强制/必需选项。因此,如果我点击提交按钮,它会发送消息,甚至没有得到验证(没有点击“我不是机器人”),这使得它无用。

代码:

<?php
$page_id = 4;
$page_details = get_page_details($page_id);
ini_set(sendmail_from, "");
require ("class.phpmailer.php");

$mail = new PHPMailer();

if ($_POST["action"] == "sendmail_contact"){
  $from = $_POST["email"];
  $contact_data = mysql_fetch_array(mysql_query("select * from contact_info"));
  $to = $contact_data["email"];
  $check_dir = "contact_files/uploads/folder" . $_SESSION["UID_Contact"];
  $results_array = array();
  if (is_dir($check_dir)){
      if ($handle = opendir($check_dir)){
          while (($file = readdir($handle)) !== FALSE){
              if ($file != "Thumbs.db" and $file != "." and $file != ".."){$results_array[] = $file;}}
closedir($handle);}}
if ($results_array[0] != ""){
      $thumbpath1 = $results_array[0];
      $mail->AddAttachment("$check_dir/$thumbpath1");}   
  if ($results_array[1] != ""){
      $thumbpath2 = $results_array[1];
      $mail->AddAttachment("$check_dir/$thumbpath2");}
  if ($results_array[2] != ""){
      $thumbpath3 = $results_array[2];
      $mail->AddAttachment("$check_dir/$thumbpath3");}
  if ($results_array[3] != ""){
      $thumbpath4 = $results_array[3];
      $mail->AddAttachment("$check_dir/$thumbpath4");}
  $subject = 'Contact Us Page';
  $Message = "";
  $Message.= '<font face="Verdana" size="2"><strong>IP : </strong> ' . $ip_address . '</font><br /><br />';
  $Message.= '<font face="Verdana" size="2"><strong>Country : </strong> ' . $country . '</font><br /><br />';
  $Message.= '<font face="Verdana" size="2"><strong>Heard from : </strong> ' . $_POST["where"] . '</font><br /><br /><br />';
  $Message.= '<font face="Verdana" size="2"><strong>Name : </strong> ' . ucfirst($_POST["name"]) . '</font><br /><br />';
  $Message.= '<font face="Verdana" size="2"><strong>E-mail : </strong> ' . $_POST["email"] . '</font><br /><br />';
  $Message.= '<font face="Verdana" size="2"><strong>Message : </strong> ' . $_POST["details"] . '</font><br /><br />';
  $Message1.= $Message;
  $mail->From = $from;
  $mail->FromName = $from;
  $mail->AddAddress($to);
  $mail->IsHTML(true);
  $mail->Subject = $subject;
  $mail->Body = $Message1;
  $mail->Send();
  header("location: $site_url/contact-thank-you");}
?>
<head>
<script type="text/javascript">
var $j_123 = jQuery.noConflict();
$j_123(document).ready(function() {
    var validator = $j_123("#contactusfrm").validate({
        rules: {
            name: "required",
            email: {required: true,email: true },
            details: "required"
        },
        messages: {
            name: "<span class=\"alert_contact\">required</span>",
            email: {required: "<span class=\"alert_contact\">required</span>",email: "<span class=\"alert_contact\">invalid</span>" },
            details: "<span class=\"alert_contact\">required</span>"
        }
    });
}); < /script> 

<script src='google.com/recaptcha/api.js'></script>
</head>
<body>
<div class="content">

<form id="contactusfrm" name="contactusfrm" method="post" action="url/contact">
<input type="hidden" name="action" value="sendmail_contact"/>

Your Name *<input name="name" type="text" class="textbox" id="name" title="Name"/>

Your Email *<input name="email" type="text" class="textbox" id="email" title="Email"/>

How did you hear about us?<select name="where" class="dropdown" id="where" title="How did you hear about us">
<option value="-">Click to Choose...</option>
<option value="Google Search"> Google Search </option>
<option value="Social Media"> Social Media </option>
<option value="Blog"> Blog </option>
<option value="Recommendation"> Recommendation </option>
<option value="Advertisement"> Advertisement </option>
<option value="Forum"> Forum </option>
<option value="Other"> Other </option>
</select>

Your Message *<br/><textarea name="details" class="textarea" id="details" title="Message"></textarea>

<div id="fileuploader"><div class="ajax-file-upload">UPLOAD FILE</div></div>
<script  type="text/javascript">
var $jj = jQuery.noConflict();
$jj(document).ready(function(){
$jj("#fileuploader").uploadFile({
url:"<?php
echo $site_url ?>/contact_files/upload.php",  
fileName:"myfile",
multiple:true,
returnType:"json",
showDelete:true,
deleteCallback:function(data, pd){
for(var i =0; i < data.length; i++){
$jj.post("<?php
echo $site_url ?>/contact_files/delete.php",{op:"delete",name: data[i]},
function(resp,textStatus, jqXHR){});}}});});
</script>

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

<div class="bot-cont"><button name="submit_button" type="submit" class="submit_button" value="SUBMIT MESSAGE">SUBMIT MESSAGE</button></div>

</form>
</div>

<script type="text/javascript" src="url/jquery.uploadfile.min.js"></script>
<script type="text/javascript" src="ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"></script>

</body>

此外,在我的表单中,当用户成功提交消息时,他/她将被重定向到另一个页面(谢谢页面),因此我不需要任何“成功”验证码消息。

只有当用户尝试提交而不检查“我不是机器人”时,我才需要带有“必需”文本的错误消息(就像我已经为其他字段提供的那样)。< / p>

有谁知道如何正常工作?

1 个答案:

答案 0 :(得分:0)

处理目标时,您没有检查“g-recaptcha-response”变量。看看这个:

if(isset($_POST['submit']) && !empty($_POST['submit'])):
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
      //your site secret key
      $secret = '9LuDh9kyetYYYYdT0jsVckScsH8Ks3KA';
      //get verify response data
      $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
      $responseData = json_decode($verifyResponse);
      if($responseData->success):
        //contact form submission code
        $name = !empty($_POST['name'])?$_POST['name']:'';
        $email = !empty($_POST['email'])?$_POST['email']:'';
        $message = !empty($_POST['message'])?$_POST['message']:'';

        $to = 'contact@codexworld.com';
        $subject = 'New contact form have been submitted';
        $htmlContent = "
            <h1>Contact request details</h1>
            <p><b>Name: </b>".$name."</p>
            <p><b>Email: </b>".$email."</p>
            <p><b>Message: </b>".$message."</p>
        ";
        // Always set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        // More headers
        $headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
        //send email
        @mail($to,$subject,$htmlContent,$headers);

        $succMsg = 'Your contact request have submitted successfully.';
      else:
        $errMsg = 'Robot verification failed, please try again.';
      endif;
  else:
    $errMsg = 'Please click on the reCAPTCHA box.';
  endif;
else:
  $errMsg = '';
  $succMsg = '';
endif;

上面的示例确保传递了g-recaptcha-response,检查响应,然后如果验证码检查未成功,则显示验证错误。使用此示例修改代码以检查recaptcha成功使用。