[英]Having trouble getting Google Captcha working
我目前正在創建聯系表單,該表單需要填寫驗證碼以減少垃圾郵件,我遵循了有關如何在代碼中使用驗證碼的教程,但是每次都不起作用時,希望有人可以在此提供幫助。 任何幫助,將不勝感激。
HTML
<body>
<hgroup>
<h1>Contact Us</h1>
<?php if(isset($_GET['CaptchaPass'])){ ?>
<h3>Your message was sent, you should recieve an email back within 24
hours.</h3>
<?php } ?>
<?php if(isset($_GET['CaptchaFail'])){ ?>
<h3>Captcha Failed. Please try again!</h3>
<?php } ?>
</hgroup>
<form method='post' action='contactver.php'>
<div class="group">
<input type="text" name="name"><span class="highlight"></span><span
class="bar"></span>
<label>Name</label>
</div>
<div class="group">
<input type="email" name="email"><span class="highlight"></span><span
class="bar"></span>
<label>Email</label>
</div>
<div class="group">
<input type="phone" name="phone"><span class="highlight"></span><span
class="bar"></span>
<label>Phone No.</label>
</div>
<div class="group">
<input type="message" name="message"><span class="highlight"></span><span
class="bar"></span>
<label>Message</label>
</div>
<div class="g-recaptcha" data-
sitekey="My Key, want to keep private :)"></div>
<button type="submit" name="login" class="button buttonBlue">Submit Message
<div class="ripples buttonRipples"><span class="ripplesCircle"></span></div>
</button>
</form>
PHP
$firstname = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
if(isset($_POST['login'])) {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = "Want to keep this private :)";
$response = file_get_contents($url."?
secret".$privatekey."&response=".$_POST['g-recaptcha-
response']."&remoteip".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);
if(isset($data->success) AND $data->success==true) {
echo("Pass");
}else {
header('Location: contact.php?CaptchaFail=true');
echo("Fail");
}
}
您還可以在表單提交之前使用JavaScript進行Google驗證碼驗證。
var v = grecaptcha.getResponse();
if(v.length == 0){
document.getElementById('captcha').innerHTML="You can't leave Captcha Code empty";
error = false;
}else{
document.getElementById('captcha').innerHTML="";
}
if(error !== false){
$('#yourformid').submit();
}
return false;
我相信這條線有問題:
if(isset($data->success) AND $data->success==true)
它應該是:
if(isset($data->success) AND $data->success===true)
除此之外,我建議使用Google Composer庫( https://github.com/google/recaptcha )來驗證Recaptcha。 它將解決您的代碼行和頭疼問題,因為它們將使它們保持最新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.