PEAR Packages: Captcha on PHP

At gotext.org, we are asking why don’t we have a Contact Form for users so I decided (after some Bluesman asks :p) to coded it… My first thought was “Oh, I need to work with CAPTCHAS“… But the problem was solved more easier than what I thought!

Detect if the system is human or not!

Detect if the system is human or not!

I had installed the pear package on my ubuntu system:

sudo apt-get install php-pear

Then I had installed the Text Captcha package on pear, puting on my ubuntu:

sudo pear install Text_CAPTCHA-0.4.0

Then… The code part:

require_once ‘Text/CAPTCHA.php’;
session_start();
$captcha = Text_CAPTCHA::factory(’Equation’);

$ret = $captcha->init();
if (PEAR::isError($ret)) {
echo $captcha->getMessage();
exit(1);
}

if (isset($_POST['captcha']) && isset($_SESSION['answer'])) {

if ($_POST['captcha'] != $_SESSION['answer']) {$errors[] = 'Wrong solution! Please, Try Again';}else{$errors[] = 'You're right! Everything went fine'; }
if (!empty($errors)) {
foreach ($errors as $error) {
print "<h4><font color='red'>$error</font></h4><br />";
}
}

print '
<form name="capter" action="contact_form.php" method="post">
<table>
<tr>
<th>What is this result?: '.$captcha->getCAPTCHA().'</th>
<td><input type="text" value="" name="captcha" /></td>
</tr>
<tr>
<th/>
<td><input type="submit" value="Send!" /></td>
</tr>
</form>
';
$_SESSION['answer'] = $captcha->getPhrase();
?>

Making it simplier... The only thing that I have done is create the captcha object, used the gethPrase function to get and save on the session cookies the solution, and getCaptcha to print the equation!

Simple don't you think?

3 Responses to “PEAR Packages: Captcha on PHP”

  1. carpincho says:

    dude, the font is all screw up
    any screenshot?

  2. cesarbernardini says:

    What do you mean? I see the fonts correctly, over Linux and Windows…

    I have uploaded a screenshot. Anyway, the method I have used is not the best, it’s really easy to break up :P

  3. wow gold says:

    This is my first time comment at your blog.
    Good recommended website.

Leave a Reply