Implement Google like CAPTCHA in PHP
anoopsachari | Sep 20, 2010 | Comments 0
Implementing captcha in PHP is simple with reCaptcha. Recently i came accross the captcha used by google. It’s pretty neat and readable. I searched for a service that would provide the same as reCaptcha does, but couldn’t find one.
During my search i came accross a class file written by Jose Rodriguez which was simple to use and gave me the output i was looking for.
The class file also contained following config settings. Change it as required.
See a Live Demo and Source Code
You need to integrate captcha with your form . Use the following sample code.
index.php
<?php session_start();
if (!empty($_REQUEST['captcha']))
{
if (empty($_SESSION['captcha']) || trim(strtolower($_REQUEST['captcha'])) != $_SESSION['captcha'])
{
$note= 'You Missed the Captcha. Try Again.';
}
else
{
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$message=htmlentities($_POST['message']);
// Insert values to your database .
$note= 'Comment Posted Successfully.';
}
}
unset($_SESSION['captcha']);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>achari.in demo - google captcha using php</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
body{
font-family:Arial, Helvetica, sans-serif;
font-size:13px;
}
</style>
</head>
<body>
<label><a href="#">back to tutorial</a></label>
<div style="width:300px;margin-left:auto;margin-right:auto">
<div style="width:300px;background:#ff99ff; margin-bottom:20px"><?php echo $note; ?></div>
<form method="post" >
<b>Message</b><br/>
<textarea name="message" rows="5" cols="30"></textarea><br/>
<img src="captcha.php" id="captchaImage" /><br/>
<a href="#" onclick="document.getElementById('captchaImage').src='captcha.php?'+Math.random();document.getElementById('captchaField').focus();" id="change-image">click if text not readable.</a>
<br/><br/>
<b>Input Captcha</b><br/>
<input type="text" name="captcha" id="captchaField" style="width:150px;"/><br/>
<br>
<input type="submit" value="I am Human" style="height:30px;"/>
</form>
</div>
</body>
</html>
Hope this helped you !
About the Author: a holistic web developer , movie buff and technical blogger from queen of arabian sea.







