Project 19: for CNIT 120 reCAPTCHA (15 pts.)

What You Need

You need a Web server. I used BackTrack Linux 5 R1.

Starting the Web Server

Start your machine as usual. Open a Terminal window.

In a Terminal window, enter this command, and then press Enter:

ping samsclass.info

Make sure you are getting replies. If you are not, you need to correct your networking problems before proceeding.

Starting Apache

At the #> prompt, enter these commands followed by the Enter key:
/etc/init.d/apache2 restart

netstat -an | more

You should see the local address 0.0.0.0:80 in a State of LISTEN, as shown below:

Testing PHP

On your Linux Web aerver, in a Terminal window, enter these commands, pressing Enter after each one:
cd /var/www

nano test.php

In nano, type in the code shown below:
<?php phpinfo(); ?>

Your screen should look like this:

Press Ctrl+X, then press Y, then press the Enter key. This saves your file.

From the menu bar in the top left of the BackTrack desktop, click Applications, Internet, Firefox Web Browser.

In the Firefox address bar, enter localhost/test.php and then press the Enter key. You should see a PHP configuration page, as shown below:

This verifies that Apache and PHP are running correctly.

Making a reCAPTCHA Account

Open a Web browser and go to

http://www.google.com/recaptcha/whyrecaptcha

Click the "Sign up now!" button.

A Google login page appears. Log in with a Gmail account.

In the next page, enter a domain of "YOURNAME.example.com", and check the "Enable this key on all domains (global key)" box, as shown below:

Click the "Create Key" button.

The next page shows your Public Key and Private Key, as shown below (I have truncated the keys in my example). Copy them to a Notepad file and save it--if you forget these keys you won't be able to complete the project.

Making a Form

On your Linux Web server, in a Terminal window, enter this command, and then press Enter:
nano /var/www/YOURNAME-form.html

Replace the text "YOURNAME" with your own name, but don't use any spaces.

Enter this HTML code into the form, replacing YOURNAME with your name in two places, and replacing KEY with your Public Key in two places:

<html>
<head><title>YOURNAME reCAPTCHA Form</title></head>
<body>
<h1>YOURNAME reCAPTCHA Form</h1>

<form method="post" action="captcha.php">
<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=KEY">
</script>
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=KEY" height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
<input type="submit" />
</form>
</body>
</html>

Save the file with Ctrl+X, Y, Enter.

Downloading and Extracting the Library

You need a PHP library file, which you will get from Google.

On your Linux Web aerver, in a Terminal window, enter these commands, pressing Enter after each one:

cd /var/www

wget http://recaptcha.googlecode.com/files/recaptcha-php-1.11.zip

apt-get install unzip

unzip recaptcha-php-1.11.zip

mv recaptcha-php-1.11/recaptchalib.php .

chmod a+rx recaptchalib.php

Making the PHP Processor Script

On your Linux Web server, in a Terminal window, enter this command, and then press Enter:
nano /var/www/captcha.php

Enter this PHP code into the file, replacing KEY with your Private Key, and YOURNAME with your own name:

<?php
require_once('/var/www/recaptchalib.php');
$privatekey = "KEY";
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
echo "<h1>YOURNAME reCAPTCHA Succeeded!</h1>";
}

?>

Save the file with Ctrl+X, Y, Enter.

On your Linux Web server, in a Terminal window, enter this command, and then press Enter:

chmod a+rx captcha.php

Testing the Form

From the menu bar in the top left of the BackTrack desktop, click Applications, Internet, Firefox Web Browser.

In the Firefox address bar, enter localhost/YOURNAME-form.html and then press the Enter key. You should see a reCAPTCHA form, as shown below:

Saving the Screen Image

Make sure the reCAPTCHA box is visible, as shown above.

Save a screen capture with a filename of "Proj 19a from YOUR NAME".

Fill in the CAPTCHA text and press Enter.

You should see the message "YOURNAME reCAPTCHA Succeeded!", as shown below.

Saving the Screen Image

Make sure the "YOURNAME reCAPTCHA Succeeded!" message is visible, as shown above.

Save a screen capture with a filename of "Proj 19b from YOUR NAME".

Turning In Your Project

Email the images to [email protected] with a subject of "Project 19 from YOUR NAME".


Sources

http://inko9nito.wordpress.com/2007/12/12/installing-recaptcha-with-php/

Last modified: 11-30-11 3 pm