在这里,我尝试使用谷歌api给用户一个图片验证码。但问题是即使用户点击提交即使没有提交验证码用户注册.. !! 这里有 .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);
}
?>
答案 0 :(得分:1)
我使用更清晰,更排序的代码行创建了演示。
<强> 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。
如果对此有任何疑虑,请告诉我。希望这有帮助。!
答案 1 :(得分:0)
只需对客户端的空验证码字段进行JavaScript验证,如果未填写表格,则允许他们提交表格!这里有一个很好的例子:ReCaptcha Validation