简体   繁体   中英

Having trouble getting Google Captcha working

I'm currently creating a contact form and the form requires a captcha to be completed to reduce spam, I've followed tutorials on how to use the captchas in my code, but yet everytime its not working, hopefully someone can help here. Any help would be appreciated.

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");
}


}

You can also use JavaScript for google captcha validation before form submit .

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;

I believe there is an issue with this line:

if(isset($data->success) AND $data->success==true)

It should be:

if(isset($data->success) AND $data->success===true)

Other than that, I would suggest using the Google Composer library ( https://github.com/google/recaptcha ) to validate the recaptcha. It will solve you lines of code and headaches since they will keep it up to date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM