Skip to content

Instantly share code, notes, and snippets.

@darkcolonist
Created January 23, 2019 06:50
Show Gist options
  • Save darkcolonist/9c32ffdf405f99de866cd29f9ff4db22 to your computer and use it in GitHub Desktop.
Save darkcolonist/9c32ffdf405f99de866cd29f9ff4db22 to your computer and use it in GitHub Desktop.
google recaptcha implementation - simple
<?php
if(isset($_POST["firstname"])){
echo "<pre>";
echo print_r($_POST, true);
echo "</pre>";
require("recaptchalib.php");
/**
* user your own private key here!!!
* @var string
*/
$privatekey = "---YOUR-PRIVATE-KEY-HERE---";
$response = $_POST["g-recaptcha-response"];
$verify = new recaptchalib($privatekey, $response);
if ($verify->isValid() == false) {
die('you are a robot, nothing to do here...');
} else {
// Your code here to handle a successful verification
die('you are not a robot, sending message...');
}
}
?>
<html>
<head>
<title>recaptcha tester</title>
<style>
/* Style inputs with type="text", select elements and textareas */
input[type=text], select, textarea {
width: 100%; /* Full width */
padding: 12px; /* Some padding */
border: 1px solid #ccc; /* Gray border */
border-radius: 4px; /* Rounded borders */
box-sizing: border-box; /* Make sure that padding and width stays in place */
margin-top: 6px; /* Add a top margin */
margin-bottom: 16px; /* Bottom margin */
resize: vertical /* Allow the user to vertically resize the textarea (not horizontally) */
}
/* Style the submit button with a specific background color etc */
input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
/* When moving the mouse over the submit button, add a darker green color */
input[type=submit]:hover {
background-color: #45a049;
}
/* Add a background color and some padding around the form */
.container {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
</style>
</head>
<body>
<div class="container">
<form action="index.php" method="post">
<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">
<label for="country">Country</label>
<select id="country" name="country">
<option value="australia">Australia</option>
<option value="canada">Canada</option>
<option value="usa">USA</option>
</select>
<label for="subject">Subject</label>
<textarea id="subject" name="subject" placeholder="Write something.." style="height:200px"></textarea>
<!-- use your own site key here!!! -->
<div class="g-recaptcha" data-sitekey="---YOUR-PUBLIC-KEY-HERE---"></div>
<input type="submit" value="Submit">
</form>
</div>
<script src='https://www.google.com/recaptcha/api.js'></script>
</body>
</html>
<?php
# PHPreCAPTCHA v0.1
# GNU General Public License v3.0
# This is a PHP library for Google's reCAPTCHA 2.0
# Created by Martin Georgiev, geeorgiev[at]gmail.com
# Web: www.viziongames.com
/**
* recaptchalib class
*/
class recaptchalib
{
/**
* @var string
*/
protected $secret;
/**
* @var string
*/
protected $response;
/**
* @var string
*/
protected $URL;
function __construct($secret, $response)
{
$this->secret = $secret;
$this->response = $response;
$this->URL = 'https://www.google.com/recaptcha/api/siteverify';
}
/**
* Validating reCAPTCHA response
* Response is collected from $_POST["g-recaptcha-response"]
*
* @param string $response
* @return booleans
*/
public function isValid()
{
$data = array(
'secret' => $this->secret,
'response' => $this->response
);
$options = array(
'http' => array (
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$verify = file_get_contents($this->URL, false, $context);
return $this->fromJson($verify);
}
/**
* Return response from the expected JSON returned by the service.
*
* @param string $json
* @return string
*/
public function fromJson($json)
{
$responseData = json_decode($json, true);
if (!$responseData) {
return false;
}
$hostname = isset($responseData['hostname']) ? $responseData['hostname'] : null;
if (isset($responseData['success']) && $responseData['success'] == true) {
return $responseData['success'];
}
if (isset($responseData['error-codes']) && is_array($responseData['error-codes'])) {
return false;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment