Quickly get Captcha on your PHP site

Published on
15,235 Points
4 Endorsements
Last Modified:
Community Pick
If you're like me and don't have tons of time to discover and implement new things/features on your website then you'll like this quick and easy example to keep automated bots from exploiting your web application forms. In just a couple of steps I'll walk you through on how to get the Captcha tool on your website.
screen shot of CaptchaThe beauty of Captcha is that it is free and requires virtually zero of your server space. Installation is as easy as a couple lines of code in your form's page and adding the captcha library php file to your directory. Then one simple if - then statement determines what the application should do depending on whether the user entered an appropriate captcha or not.

To get started head on over to http://www.google.com/recaptcha/whyrecaptcha to sign up (for free of course) and get your public and private keys (which you'll use in the next step). Once you're all signed up all you'll need is the recaptchalib.php file to put into your directory.

If you have successfully signed up and have the recaptchalib.php file and a private and public key you're ready to finish up.

First of all let's start with a simple php page (form.php) with a form that submits the inputted text on another page (success.php) if the captcha entry was valid; if not, then we show an error message below the captcha on the form page.

This example asks the user to input his favorite food and insert the Captcha. If the Captcha is correct, the user will be directed to a success page that shows his entry - otherwise an error message is displayed telling the user that the Captcha was entered incorrectly.

<form action="form.php" method="post">
            Favorite Food: <input type="text" id="favorite_food" name="favorite_food" value="" size="40" />
            <br /><br />
            $publickey = "your public key goes here...";
            $privatekey = "your private key goes here...";
            echo recaptcha_get_html($publickey, $error);
            <br />
            <input type="submit" value="Submit" />
        <br />
        if ($_POST["favorite_food"]) { //checks to see if the form has been submitted
            $response = recaptcha_check_answer($privatekey, // captcha's function to validate input
            if ($response->is_valid) {
                $fav_food = $_POST['favorite_food'];
            } else {
                # set the error code in GREEN!
                echo "<p style='color: #95ca05; font-size: larger; font-family: 'tempus sans itc';>You entered the Captcha incorrectly.</p><br /><br />";

Open in new window

Your favorite food is: <?php echo($_GET['fav_food']);?>

Open in new window

For this to work you need recaptchalib.php, form.php, and success.php to be in the same directory and your respective private and public keys set.

That's it, you can see how easy it is to determine what your web application will do when the captcha is correct or not. There are more customization options available if you check out http://code.google.com/apis/recaptcha/docs/customization.html and also there are 'PHP-less' solutions that I am not covering here: http://code.google.com/apis/recaptcha/docs/display.html.

If you want to see a working example of the example you can check it out here: http://www.patsmitty.com/captcha_tut/form.php

Also the source files are attached.

Author:Bruce Smith
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free