My favorites | Sign in
Project Home Wiki
Search
for
RecaptchaExample2  

Featured
Updated Feb 4, 2010 by izimo...@gmail.com

Services_ReCaptcha example 2

<?php

/**
 * Include the Services_ReCaptcha class
 */
require_once 'Services/ReCaptcha.php';

// you must get your API keys here:
// http://recaptcha.net/api/getkey
$publicKey  = 'your_public_key';
$privateKey = 'your_private_key';

// we instanciate our Services_ReCaptcha instance with the public key and the 
// private key
$recaptcha = new Services_ReCaptcha($publicKey, $privateKey);

// we are going to customize our Services_ReCaptcha instance
$recaptcha->setOption('secure', true);   // force the secure URL
$recaptcha->setOption('theme', 'white'); // use the white theme
$recaptcha->setOption('lang', 'fr');     // set language to french

// alternatively we could have done:
// $recaptcha = new Services_ReCaptcha($publicKey, $privateKey, true, array(
//     'secure' => true,
//     'theme'  => 'white',
//     'lang'   => 'fr'
// ));
// or:
// $recaptcha->setOptions(array('secure' => true, 'theme' => 'white', 'lang' => 'fr'));

// we use a proxy, so we need to configure it
$recaptcha->getRequest()->setConfig(array(
    'proxy_host' => 'localhost',
    'proxy_port' => 8118,
));

// if the form was submitted
if (isset($_POST['submit'])) {
    if ($recaptcha->validate()) {
        // the catpcha challenge response is ok, we display a message and exit
        echo "Challenge response ok !";
        exit(0);
    } else {
        // if the captcha validation failed, instead of letting the captcha 
        // display the error, we want to echo the error and exit
        echo $recaptcha->getError();
        exit(1);
    }
}

// we display the html form
?>
<html>
<head>
    <title>recaptcha test</title>
</head>
<body>
    <form method="post" action="">
<?php echo $recaptcha; ?>
        <hr/>
        <input type="submit" name="submit" value="Ok"/>
    </form>
</body>
</html>

Sign in to add a comment
Powered by Google Project Hosting