Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
<li><h2>General</h2>
<ul>
<li><a href="/recaptcha-content-security-policy.php">Content Security Policy</a></li>
<li><a href="/recaptcha-request-curl.php">Explicit <kbd>CurlPost</kbd></a></li>
<li><a href="/recaptcha-request-post.php">Explicit <kbd>Post</kbd></a></li>
<li><a href="/recaptcha-request-socket.php">Explicit <kbd>SocketPost</kbd></a></li>
</ul>
</li>
</ul>
Expand Down
151 changes: 151 additions & 0 deletions examples/recaptcha-request-curl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod\CurlPost;

/**
* BSD 3-Clause License.
*
* @copyright (c) 2019, Google Inc.
*
* @see https://www.google.com/recaptcha
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

require __DIR__.'/appengine-https.php';

// Initiate the autoloader. The file should be generated by Composer.
// You will provide your own autoloader or require the files directly if you did
// not install via Composer.
require_once __DIR__.'/../vendor/autoload.php';

// Register API keys at https://www.google.com/recaptcha/admin
$siteKey = '';
$secret = '';

// Copy the config.php.dist file to config.php and update it with your keys to run the examples
if (is_readable(__DIR__.'/config.php')) {
/** @var array{'v2-standard': array{site: string, secret: string}} $config */
$config = include __DIR__.'/config.php';
$siteKey = $config['v2-standard']['site'];
$secret = $config['v2-standard']['secret'];
}

// reCAPTCHA supports 40+ languages listed here: https://developers.google.com/recaptcha/docs/language
$lang = 'en';
?>
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,height=device-height,minimum-scale=1">
<link rel="shortcut icon" href="https://www.gstatic.com/recaptcha/admin/favicon.ico" type="image/x-icon"/>
<link rel="canonical" href="https://recaptcha-demo.appspot.com/recaptcha-request-curl.php">
<script type="application/ld+json">{ "@context": "http://schema.org", "@type": "WebSite", "name": "reCAPTCHA demo - Explicit CurlPost", "url": "https://recaptcha-demo.appspot.com/recaptcha-request-curl.php" }</script>
<meta name="description" content="reCAPTCHA demo - Explicit CurlPost" />
<meta property="og:url" content="https://recaptcha-demo.appspot.com/recaptcha-request-curl.php" />
<meta property="og:type" content="website" />
<meta property="og:title" content="reCAPTCHA demo - Explicit CurlPost" />
<meta property="og:description" content="reCAPTCHA demo - Explicit CurlPost" />
<link rel="stylesheet" type="text/css" href="/examples.css">
<title>reCAPTCHA demo - Explicit CurlPost</title>

<header>
<h1>reCAPTCHA demo</h1><h2>Explicit <kbd>CurlPost</kbd></h2>
<p><a href="/"><span aria-hidden="true">↩️</span> Home</a></p>
</header>
<main>
<?php
if ('' === $siteKey || '' === $secret) {
?>
<h2>Add your keys</h2>
<p>If you do not have keys already then visit <kbd> <a href = "https://www.google.com/recaptcha/admin">https://www.google.com/recaptcha/admin</a></kbd> to generate them. Edit this file and set the respective keys in the <kbd>config.php</kbd> file or directly to <kbd>$siteKey</kbd> and <kbd>$secret</kbd>. Reload the page after this.</p>
<?php
} elseif (!empty($_POST[ReCaptcha::RESPONSE_KEY])) {
// The POST data here is unfiltered because this is an example.
// In production, *always* sanitise and validate your input'
?>
<h2><kbd>POST</kbd> data</h2>
<kbd><pre><?php var_export($_POST); ?></pre></kbd>
<?php
// If the form submission includes the "g-captcha-response" field
// Create an instance of the service using your secret and the CurlPost request method.
/** @var string $secret */
$recaptcha = new ReCaptcha($secret, new CurlPost());

// Make the call to verify the response and also pass the user's IP address
/** @var string $serverName */
$serverName = $_SERVER['SERVER_NAME'];

/** @var string $responseKey */
$responseKey = $_POST[ReCaptcha::RESPONSE_KEY];

/** @var null|string $remoteAddr */
$remoteAddr = $_SERVER['REMOTE_ADDR'];

$resp = $recaptcha->setExpectedHostname($serverName)
->verify($responseKey, $remoteAddr)
;
if ($resp->isSuccess()) {
// If the response is a success, that's it!
?>
<h2>Success!</h2>
<kbd><pre><?php var_export($resp); ?></pre></kbd>
<p>That's it. Everything is working. Go integrate this into your real project.</p>
<p><a href="/recaptcha-request-curl.php"><span aria-hidden="true">⤴️</span> Try again</a></p>
<?php
} else {
// If it's not successful, then one or more error codes will be returned.
?>
<h2>Something went wrong</h2>
<kbd><pre><?php var_export($resp); ?></pre></kbd>
<p>Check the error code reference at <kbd><a href="https://developers.google.com/recaptcha/docs/verify#error-code-reference">https://developers.google.com/recaptcha/docs/verify#error-code-reference</a></kbd>.
<p><strong>Note:</strong> Error code <kbd>missing-input-response</kbd> may mean the user just didn't complete the reCAPTCHA.</p>
<p><a href="/recaptcha-request-curl.php"><span aria-hidden="true">⤴️</span> Try again</a></p>
<?php
}
} else {
// Add the g-recaptcha tag to the form you want to include the reCAPTCHA element
?>
<p>Complete the reCAPTCHA then submit the form.</p>
<form action="/recaptcha-request-curl.php" method="post">
<fieldset>
<legend>An example form</legend>
<label class="form-field">Example input A: <input type="text" name="ex-a" value="foo"></label>
<label class="form-field">Example input B: <input type="text" name="ex-b" value="bar"></label>
<!-- Default behaviour looks for the g-recaptcha class with a data-sitekey attribute -->
<div class="g-recaptcha form-field" data-sitekey="<?php echo (string) $siteKey; ?>"></div>
<button class="form-field" type="submit">Submit <span aria-hidden="true">↦</span></button>
</fieldset>
</form>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl=<?php echo $lang; ?>"></script>
<?php
}?>
</main>

<!-- Google Analytics - just ignore this -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-123057962-1"></script>
<script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-123057962-1');</script>
151 changes: 151 additions & 0 deletions examples/recaptcha-request-post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod\Post;

/**
* BSD 3-Clause License.
*
* @copyright (c) 2019, Google Inc.
*
* @see https://www.google.com/recaptcha
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

require __DIR__.'/appengine-https.php';

// Initiate the autoloader. The file should be generated by Composer.
// You will provide your own autoloader or require the files directly if you did
// not install via Composer.
require_once __DIR__.'/../vendor/autoload.php';

// Register API keys at https://www.google.com/recaptcha/admin
$siteKey = '';
$secret = '';

// Copy the config.php.dist file to config.php and update it with your keys to run the examples
if (is_readable(__DIR__.'/config.php')) {
/** @var array{'v2-standard': array{site: string, secret: string}} $config */
$config = include __DIR__.'/config.php';
$siteKey = $config['v2-standard']['site'];
$secret = $config['v2-standard']['secret'];
}

// reCAPTCHA supports 40+ languages listed here: https://developers.google.com/recaptcha/docs/language
$lang = 'en';
?>
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,height=device-height,minimum-scale=1">
<link rel="shortcut icon" href="https://www.gstatic.com/recaptcha/admin/favicon.ico" type="image/x-icon"/>
<link rel="canonical" href="https://recaptcha-demo.appspot.com/recaptcha-request-post.php">
<script type="application/ld+json">{ "@context": "http://schema.org", "@type": "WebSite", "name": "reCAPTCHA demo - Explicit Post", "url": "https://recaptcha-demo.appspot.com/recaptcha-request-post.php" }</script>
<meta name="description" content="reCAPTCHA demo - Explicit Post" />
<meta property="og:url" content="https://recaptcha-demo.appspot.com/recaptcha-request-post.php" />
<meta property="og:type" content="website" />
<meta property="og:title" content="reCAPTCHA demo - Explicit Post" />
<meta property="og:description" content="reCAPTCHA demo - Explicit Post" />
<link rel="stylesheet" type="text/css" href="/examples.css">
<title>reCAPTCHA demo - Explicit Post</title>

<header>
<h1>reCAPTCHA demo</h1><h2>Explicit <kbd>Post</kbd></h2>
<p><a href="/"><span aria-hidden="true">↩️</span> Home</a></p>
</header>
<main>
<?php
if ('' === $siteKey || '' === $secret) {
?>
<h2>Add your keys</h2>
<p>If you do not have keys already then visit <kbd> <a href = "https://www.google.com/recaptcha/admin">https://www.google.com/recaptcha/admin</a></kbd> to generate them. Edit this file and set the respective keys in the <kbd>config.php</kbd> file or directly to <kbd>$siteKey</kbd> and <kbd>$secret</kbd>. Reload the page after this.</p>
<?php
} elseif (!empty($_POST[ReCaptcha::RESPONSE_KEY])) {
// The POST data here is unfiltered because this is an example.
// In production, *always* sanitise and validate your input'
?>
<h2><kbd>POST</kbd> data</h2>
<kbd><pre><?php var_export($_POST); ?></pre></kbd>
<?php
// If the form submission includes the "g-captcha-response" field
// Create an instance of the service using your secret and the Post request method.
/** @var string $secret */
$recaptcha = new ReCaptcha($secret, new Post());

// Make the call to verify the response and also pass the user's IP address
/** @var string $serverName */
$serverName = $_SERVER['SERVER_NAME'];

/** @var string $responseKey */
$responseKey = $_POST[ReCaptcha::RESPONSE_KEY];

/** @var null|string $remoteAddr */
$remoteAddr = $_SERVER['REMOTE_ADDR'];

$resp = $recaptcha->setExpectedHostname($serverName)
->verify($responseKey, $remoteAddr)
;
if ($resp->isSuccess()) {
// If the response is a success, that's it!
?>
<h2>Success!</h2>
<kbd><pre><?php var_export($resp); ?></pre></kbd>
<p>That's it. Everything is working. Go integrate this into your real project.</p>
<p><a href="/recaptcha-request-post.php"><span aria-hidden="true">⤴️</span> Try again</a></p>
<?php
} else {
// If it's not successful, then one or more error codes will be returned.
?>
<h2>Something went wrong</h2>
<kbd><pre><?php var_export($resp); ?></pre></kbd>
<p>Check the error code reference at <kbd><a href="https://developers.google.com/recaptcha/docs/verify#error-code-reference">https://developers.google.com/recaptcha/docs/verify#error-code-reference</a></kbd>.
<p><strong>Note:</strong> Error code <kbd>missing-input-response</kbd> may mean the user just didn't complete the reCAPTCHA.</p>
<p><a href="/recaptcha-request-post.php"><span aria-hidden="true">⤴️</span> Try again</a></p>
<?php
}
} else {
// Add the g-recaptcha tag to the form you want to include the reCAPTCHA element
?>
<p>Complete the reCAPTCHA then submit the form.</p>
<form action="/recaptcha-request-post.php" method="post">
<fieldset>
<legend>An example form</legend>
<label class="form-field">Example input A: <input type="text" name="ex-a" value="foo"></label>
<label class="form-field">Example input B: <input type="text" name="ex-b" value="bar"></label>
<!-- Default behaviour looks for the g-recaptcha class with a data-sitekey attribute -->
<div class="g-recaptcha form-field" data-sitekey="<?php echo (string) $siteKey; ?>"></div>
<button class="form-field" type="submit">Submit <span aria-hidden="true">↦</span></button>
</fieldset>
</form>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl=<?php echo $lang; ?>"></script>
<?php
}?>
</main>

<!-- Google Analytics - just ignore this -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-123057962-1"></script>
<script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-123057962-1');</script>
Loading
Loading