Add google reCAPTCHA in form in PHP
<html>
<head>
<script src='https://www.google.com/recaptcha/api.js' async defer></script>
</head>
<body>
<?php
if(isset($_POST['submit'])){
$captcha;
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo 'Please check the the captcha form.';
exit;
}
$secretKey = "<!==== Secret key ====!>";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
if($responseKeys["success"]) {
echo 'Thanks for posting';
echo $_POST['name'];
} else {
echo 'You are spammer ! Get the @$%K out';
}
}
?>
<form action="" method="post">
<input type="text" name="name" placeholder="name" value="Hello World">
<div class="g-recaptcha" data-sitekey="<!==== Site key ====!>"></div>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Comments
Post a Comment