This blog is about C++, CakePHP, Linux, PEAR, PHP, Pyrus, Apache, and MySQL and related topics.

Sunday, October 25, 2009

How To Install PEAR Services_Recaptcha-0.2.1

In this blog post I will show you how I installed PEAR Services_Recaptcha-0.2.1.

This is my setup:

  • Windows XP SP3
  • WampServer 2.0i
    • PHP version 5.3.0
    • Apache version 2.2.11
    • MySQL version 5.1.36
  • PEAR version 1.9.0
If you want to have a setup like mine just follow the instructions here. I have also explained How To Install PEAR with PHP 5.3.0 for those who don't want to install WampServer.

This is what we have to do:

  1. Load the pear console
  2. Login to the pear channel
  3. Services_Recaptcha-0.2.1 requires HTTP_Request2-0.4.1 which in turn requires Net_URL2-0.2.0 so we will install in this order: Net_URL2-0.2.0, HTTP_Request2-0.4.1, then Services_Recaptcha-0.2.1
  4. Test it is working
Step One: Load the pear console
Step One can be further broken down into two steps:

  1. Open the windows command line
  2. Switch to the PHP directory
1: Open the Command Prompt (also known as DOS and cmd.exe).
This is one way of doing it:

  • click start
  • click run
  • type cmd.exe in the 'open' field
  • click ok or press enter
The Command Prompt should now load and this is what it looks like:



2: Switch to the PHP directory.

This is the directory where php.exe is located. If you installed WampServer using default configurations then you will probably be going here: c:\wamp\bin\php\php5.3.0


On the command line type the following: cd c:\wamp\bin\php\php5.3.0

The prompt should now look like this c:\wamp\bin\php\php5.3.0




At this point it would be good to verify everything is going ok. Type 'pear' (without quotes) and hit enter. A list of pear commands should load.

This is what it looks like:




If this does not happen then check the following:


  • Did you spell pear correctly? This is the most obvious mistake so rule this one out first.
  • Are you in the right directory? Type 'dir' and a list of files should load. You know you are in the right directory if you see the following files:
    • php.exe
    • php.ini
    • pear.bat
    • PEAR 
    'PEAR' is a directory and not a file, but it should be there just the same.
  • Did you install PEAR (or did you install it correctly)? Go back and do it again

Step Two:  Login to the pear channel
At the prompt just type the following: pear login





  • Select '1' and enter a username
  • Select '2' and enter a password
  • Review and hit enter if everything is ok.
  • You are logged in

Step Three: Installations

 It's as simple as typing these three commands in succession:


  • pear install pear/Net_URL2-0.2.0
  • pear install pear/HTTP_Request2-0.4.1
  • pear install pear/Services_Recaptcha-0.2.1
The error messages are very helpful here. You can use them to verify which versions you should be installing. For example if you type 'pear install Services_Recaptcha' you will get an error telling you which version to install and which channel to install it from. Also if you type 'pear install pear/Services_Recaptcha-0.2.1' first you will get an error message telling you HTTP_Request2-0.4.1 should be installed and 'pear install pear/HTTP_Request2-0.4.1' will give you an error message that Net_URL2-0.2.0 should be installed.

It would also be a good idea to check http://pear.php.net to make sure you are installing the most recent version of Services_Recaptcha and its dependencies.

Step Four: Test it is working

To test it is working we will use some test code. Just cut and paste the following code and save as recaptcha.php and put this file in your DocumentRoot (c:\wamp\www if you installed WampServer with default configuration)

 This code comes from: http://code.google.com/p/services-recaptcha/wiki/RecaptchaExample2

Here is the code:



<?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>


Get the Recaptcha Keys


Next we go to  http://recaptcha.net/api/getkey and get a public and private key to insert into the script (around lines 10-11). For development purposes make global keys. In other words, we don't intend to use these keys in the real world and we don't want to set up new keys more than once for development purposes.

Load the page into your browser. This is what you should see:



That's it! You have successfully installed PEAR package Services_Recaptcha-0.2.1

No comments:

Post a Comment

Followers