public static function createCaptcha($environment) { $captcha = new \Captcha\Captcha(); if (strtolower($environment) == 'production') { $captcha->setPublicKey('6LcwQPwSAAAAAEaSdomAVBdEa_ZcPFIENzzAaukT'); $captcha->setPrivateKey('6LcwQPwSAAAAAEITEtwbGBet_tltApNYbh0oDag9'); } if (strtolower($environment) == 'dev.peter') { $captcha->setPublicKey('6LdrO_wSAAAAAKm8_PxSJGreOdLVBAoGP2Gi3zgn'); $captcha->setPrivateKey('6LdrO_wSAAAAAMH6Ds8YRPAkcKozZX80iGXUsr50'); } return $captcha; }
function validate_captcha() { $captcha = new Captcha\Captcha(); $captcha->setPublicKey(CONFIG_RECAPTCHA_PUBLIC_KEY); $captcha->setPrivateKey(CONFIG_RECAPTCHA_PRIVATE_KEY); $response = $captcha->check(); if (!$response->isValid()) { message_error("The reCAPTCHA wasn't entered correctly. Go back and try it again."); } }
/** * Fetches the HTML for the captcha. * * @param array $configuration Configuration data. * * @static * @uses Core\Session() * * @return \Captcha\Captcha */ public static function get(array $configuration) { $captcha = Core\Session()->get('captcha'); if (!$captcha && Core\Session()->get('authentication_error')) { $captcha = new \Captcha\Captcha(); $captcha->setPublicKey($configuration['public_key']); $captcha->setPrivateKey($configuration['private_key']); Core\Session()->set('captcha', $captcha); } return $captcha; }
require_once '../../../../vendor/autoload.php'; require_once '../utils/response.php'; require_once '../utils/session.php'; require_once 'form.php'; Response::allow_method('POST'); session_start(); if (!isset($_POST['token']) || !Session::verify_token('token_form_contact', $_POST['token'])) { Response::json(401, 'La sesión es inválida o este formulario ya fue enviado.'); } unset($_POST['token']); if (!isset($_POST['recaptcha']) || !isset($_POST['recaptcha']['challenge']) || !isset($_POST['recaptcha']['response'])) { Response::json(400, 'Captcha requerido!'); } // Validate captcha $captcha = new Captcha\Captcha(); $captcha->setPublicKey(Config::$RECAPTCHA_PUBLIC); $captcha->setPrivateKey(Config::$RECAPTCHA_SECRET); $response = $captcha->check($_POST['recaptcha']['challenge'], $_POST['recaptcha']['response']); if (!$response->isValid()) { Response::json(401, 'Captcha inválido!'); } unset($_POST['recaptcha']); // Form creation $MAX_LENGTH_NAME = 30; $MAX_LENGTH_SUBJECT = 70; $MAX_LENGTH_MESSAGE = 250; $REASONS = array('enquiry' => 'Consultas', 'complaint' => 'Reclamos', 'difussion' => 'Bandas/Difusión', 'other' => 'Otros'); $form = new Form(array('full_name' => array('label' => 'Nombre/s', 'type' => 'string', 'required' => true, 'maxlength' => $MAX_LENGTH_NAME), 'email' => array('label' => 'Correo electrónico', 'type' => 'email', 'required' => true), 'reason' => array('label' => 'Motivo', 'type' => 'string', 'required' => true, 'select' => $REASONS), 'subject' => array('label' => 'Asunto', 'type' => 'string', 'required' => true, 'maxlength' => $MAX_LENGTH_SUBJECT), 'message' => array('label' => 'Mensaje', 'type' => 'string', 'required' => true, 'maxlength' => $MAX_LENGTH_MESSAGE))); $errors = $form->validate_whitelist(); if (!empty($errors)) { Response::json(400, $errors);
function display_captcha() { echo ' <script type="text/javascript"> var RecaptchaOptions = { theme : "clean" }; </script> '; $captcha = new Captcha\Captcha(); $captcha->setPublicKey(CONFIG_RECAPTCHA_PUBLIC_KEY); $captcha->setPrivateKey(CONFIG_RECAPTCHA_PRIVATE_KEY); echo $captcha->html(); }
<?php /** * You must run `composer install` in order to generate autoloader for this example */ require __DIR__ . '/../vendor/autoload.php'; // New captcha instance $captcha = new Captcha\Captcha(); $captcha->setPublicKey('publickey'); $captcha->setPrivateKey('privatekey'); // set a remote IP if the remote IP can not be found via $_SERVER['REMOTE_ADDR'] if (!isset($_SERVER['REMOTE_ADDR'])) { $captcha->setRemoteIp('192.168.1.1'); } // Output captcha to end user echo $captcha->html(); // Perform validation (put this inside if ($_POST) {} condition for example) $response = $captcha->check(); if (!$response->isValid()) { echo $response->getError(); }