如何验证图片验证码..?

How to validate picture captcha..?

我在这里尝试使用 google api 给用户一个图片验证码..但问题是即使用户点击提交即使没有提交验证码用户也已注册..!! 这里 gose .html 文件

<form method = "POST" name = "register" id = "register" class="m-t" role="form" action="login.html">

  <div class="form-group">
    <input type="text" name = "fname" id = "fname" class="form-control" placeholder="First Name" required="">
  </div>
  <div class="form-group">
    <input type="text" name = "lname" id = "lname" class="form-control" placeholder="Last Name" required="">
  </div>
  <div class="form-group">
    <input type="email" name = "email" id = "email" class="form-control" placeholder="Email" required="">
  </div>
  <div class="form-group">
    <input type="password" name = "password" id = "password" class="form-control" placeholder="Password" required="">
  </div>
  <div class="form-group">
    <input type="mobile" name = "mobile" id = "mobile" class="form-control" placeholder="Mobile No" required="">
  </div>
  <div  class="form-group" id="recaptcha_widget">
    <div class="required">
      <div class="g-recaptcha" data-sitekey="6Lc4vP4SAAAAABjhRjyoMguw66mNSBgdpBF398AG"></div>
        <!-- End Thumbnail-->
      </div>
      <?php include("js/captcha.php");?>
    </div>
    <div class="form-group">
      <div cle the terms and policy </label></div>
    </div>ass="checkbox i-checks"><label> <input type="checkbox"><i></i> Agre
    <button type="submit" name = "submit" id = "submit" class="btn btn-primary block full-width m-b">Register</button>

    <p class="text-muted text-center"><small>Already have an account?</small></p>
    <a class="btn btn-sm btn-white btn-block" href="login.html">Login</a>
</form>

这里是 .php 文件

<?php
  if(isset($_POST['submit'])){
    // Send data and get response
    $cap = $_POST['g-recaptcha-response'];
    $ch = curl_init('https://www.google.com/recaptcha/api/siteverify?secret=6Lc4vP4SAAAAAGOM8ERb1pYSBfHjiMGb9bnGVtog&response='.$cap);                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                                                                               
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    if($result = curl_exec($ch);
      curl_close($ch);

      print_r($result);
    }
?>

只需在客户端对空的验证码字段进行 JavaScript 验证,如果未填写则允许他们提交表单!这里有一个很好的例子:ReCaptcha Validation

我用更清晰、更有序的代码行创建了演示。

HTML:

<html>
  <head>
    <title>Addweb solution Pvt Ltd Google recapcha demo - Codeforgeek</title>
    <script src='https://www.google.com/recaptcha/api.js'></script>
  </head>
  <body>
    <h1>Google reCAPTHA Demo</h1>
    <form id="comment_form" action="verify.php" method="post">
      <input type="email" placeholder="Type your email" size="40"><br><br>
      <textarea name="comment" rows="8" cols="39"></textarea><br><br>
      <input type="submit" name="submit" value="Post comment"><br><br>
      <div class="g-recaptcha" data-sitekey="XXXXXXXXX_SITE_KEY_XXXXXXXX"></div>
    </form>

  </body>
</html>

PHP:

<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])){
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
        //your site secret key
        $secret = 'XXXXXXXXXXXXX_SECRET_KEY_XXXXXXXXXXXX';
        //get verify response data

        $param = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response'];
        $verifyResponse = file_get_contents($param);
        $responseData = json_decode($verifyResponse);

        if($responseData->success){
            //contact form submission code goes here
            $succMsg = 'Your contact request have submitted successfully.';
            //Your PHP stuff for use after captcha validate success.

        }else{
            $errMsg = 'Robot verification failed, please try again.';
        }
    }else{
        $errMsg = 'Please click on the reCAPTCHA box.';
    }
}
?>

请将您的站点密钥替换为“XXXXXXXXX_SITE_KEY_XXXXXXXX”,将密钥替换为“XXXXXXXXXXXXX_SECRET_KEY_XXXXXXXXXXXX”。还将您的 mysql 相关逻辑用于成功验证块。

以后你需要检查错误信息,如果有的话你可以找到here

如果对此有任何疑虑,请告诉我。希望对您有所帮助!