jcadima
5/26/2017 - 9:20 PM

Simple recaptcha demo

Simple recaptcha demo


<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])):
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
        //your site secret key
        $secret = '6LeT7h8UAAAAAOoLSh0t6tPgv8D3hCMUeJN5FxRY';
        //get verify response data
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        if($responseData->success):
            //contact form submission code
            $name = !empty($_POST['name'])?$_POST['name']:'';
            $email = !empty($_POST['email'])?$_POST['email']:'';
            $message = !empty($_POST['message'])?$_POST['message']:'';
            

            $htmlContent = "
                <h1>Contact request details</h1>
                <p><b>Name: </b>".$name."</p>
                <p><b>Email: </b>".$email."</p>
                <p><b>Message: </b>".$message."</p>
            ";
            
            $succMsg = 'Your contact request have submitted successfully with the following data:<br>';
            $succMsg .= $htmlContent ;
        else:
            $errMsg = 'Robot verification failed, please try again.';
        endif;
    else:
        $errMsg = 'Please click on the reCAPTCHA box.';
    endif;
else:
    $errMsg = '';
    $succMsg = '';
endif;
?>

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title></title>

  <link rel="stylesheet" href="css/main.css">
  <script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>

<body>

<?php 
if ( isset($succMsg) ) { echo $succMsg  ; } 
if ( isset($errMsg) ) { echo $errMsg  ; } 
?>

<form action="" method="POST">
    <input type="text" name="name" value="" placeholder="Name">
    <input type="text" name="email" value="" placeholder="Email">
    <div><textarea type="text" name="message" placeholder="Message"></textarea></div>
    <div class="g-recaptcha" data-sitekey="6LeT7h8UAAAAALq7qjy90QTcjphFpAtg3oW6zEcT"></div>
    <input type="submit" name="submit" value="SUBMIT">
</form>

	

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

</body>

</html>