Services_ReCaptcha
[ class tree: Services_ReCaptcha ] [ index: Services_ReCaptcha ] [ all elements ]

Source for file example-02.php

Documentation is available at example-02.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * This file is part of the PEAR Services_ReCaptcha package.
  7.  *
  8.  * PHP version 5
  9.  *
  10.  * LICENSE: This source file is subject to the MIT license that is available
  11.  * through the world-wide-web at the following URI:
  12.  * http://opensource.org/licenses/mit-license.php
  13.  *
  14.  * @category  Services
  15.  * @package   Services_ReCaptcha
  16.  * @author    David Jean Louis <izi@php.net>
  17.  * @copyright 2008-2009 David Jean Louis
  18.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  19.  * @version   CVS: $Id$
  20.  * @link      http://pear.php.net/package/Services_ReCaptcha
  21.  * @link      http://recaptcha.net/apidocs/captcha/client.html
  22.  * @since     File available since release 0.1.0
  23.  * @filesource
  24.  */
  25.  
  26. /**
  27.  * Include the Services_ReCaptcha class
  28.  */
  29. require_once 'Services/ReCaptcha.php';
  30.  
  31. // you must get your API keys here:
  32. // http://recaptcha.net/api/getkey
  33. $publicKey  'your_public_key';
  34. $privateKey 'your_private_key';
  35.  
  36. // we instanciate our Services_ReCaptcha instance with the public key and the 
  37. // private key
  38. $recaptcha = new Services_ReCaptcha($publicKey$privateKey);
  39.  
  40. // we are going to customize our Services_ReCaptcha instance
  41. $recaptcha->setOption('secure'true);   // we force the secure url
  42. $recaptcha->setOption('theme''white')// use the white theme
  43. $recaptcha->setOption('lang''fr');     // set language to french
  44.  
  45. // alternatively we could have done:
  46. // $recaptcha = new Services_ReCaptcha($publicKey, $privateKey, array(
  47. //     'secure' => true,
  48. //     'theme'  => 'white',
  49. //     'lang'   => 'fr'
  50. // ));
  51. // or:
  52. // $recaptcha->setOptions(array('theme' => 'white', 'lang' => 'fr'));
  53.  
  54. // we use a proxy, so we need to configure it
  55. $recaptcha->getRequest()->setConfig(
  56.     array('proxy_host' => 'localhost''proxy_port' => 8118)
  57. );
  58.  
  59. // if the form was submitted
  60. if (isset($_POST['submit'])) {
  61.     if ($recaptcha->validate()) {
  62.         // the catpcha challenge response is ok, we display a message and exit
  63.         echo "Challenge response ok !";
  64.         exit(0);
  65.     else {
  66.         // if the captcha validation failed, instead of letting the captcha 
  67.         // display the error, we want to echo the error and exit
  68.         echo $recaptcha->getError();
  69.         exit(1);
  70.     }
  71. }
  72.  
  73. // we display the html form
  74. ?>
  75. <html>
  76. <head>
  77.     <title>recaptcha test</title>
  78. </head>
  79. <body>
  80.     <form method="post" action="">
  81. <?php echo $recaptcha?>
  82.         <hr/>
  83.         <input type="submit" name="submit" value="Ok"/>
  84.     </form>
  85. </body>
  86. </html>

Documentation generated on Mon, 11 Mar 2019 15:43:39 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.