Tired of those strange letters and numbers captcha ? I know how frustrating is to try reading those distorted images. Don't stress your users in the same way, try something easier to keep out those bad spam robots. Try the cat dog captcha.
Spam robots have been visiting my blog a lot lately. So, I decided to make something about it.The typical distorted letters and number are too boring, I wanted something more original, so here it is. The idea is simple. There are 6 images ( can be more or less ), in which at least one of them (maximum 3 in this example) is a cat, so the user must select all the cats to prove that is a human. Enough talk, show me the cat dog captcha demo
The php code
define('CAPTCHA_NR',6);
define('CAPTCHA_CAT',1);
define('CAPTCHA_DOG',0);
define('CAPTCHA_IMG_NR',20);
session_start();
//if we have an image request serve the image
if ( isset( $_GET['captcha'] ) )
{
header('Content-Type: image/jpeg');
if ( $_SESSION['_captcha'][ $_GET['captcha'] ] == CAPTCHA_DOG )
{
exit( readfile( dirname(__FILE__) . '/dogs/' . rand( 1, CAPTCHA_IMG_NR) . '.jpg' ) );
} else
{
exit( readfile( dirname(__FILE__) . '/cats/' . rand( 1, CAPTCHA_IMG_NR) . '.jpg' ) );
}
}
//check if the cats were checked
if ( isset( $_POST['_captcha'] ) )
{
$captcha_ok = true;
//check if the user checked the cats
for ( $i = 1; $i <= CAPTCHA_NR; $i++ )
{
//if the checkboxes are not set assume that they are dogs
if ( ! isset( $_POST['_captcha'][$i] ) ) $_POST['_captcha'][$i] = 0;
//do we have a match ?
if ( $_SESSION['_captcha'][$i] != $_POST['_captcha'][$i] )
{
$captcha_ok = false;
break;
//wrong guess
}
}
}
//generate images permutation
//make them all dogs
for ( $i = 1; $i <= CAPTCHA_NR; $i++ )
{
$_SESSION['_captcha'][$i] = CAPTCHA_DOG;
}
//add at least one cat, but no more than half of the images
for ( $i = 1; $i <= (CAPTCHA_NR / 2); $i++ )
{
$_SESSION['_captcha'][ rand(0, CAPTCHA_NR) ] = CAPTCHA_CAT;
}
The html code
p>
<b class="captcha_message">
<?php
if ( isset( $captcha_ok ) )
{
if ( $captcha_ok == true )
{
echo 'You are a human !';
}
else
{
echo 'Not a kittie lover or bad spam robot !!!';
}
}
?></b></p>
<p><label>Human test</label>Please select all cats from the images below</p>
<div id="captcha">
<?php for ( $i = 1; $i <= CAPTCHA_NR; $i++ ):?>
<div>
<input type="checkbox" name="_captcha[<?php echo $i;?>]" value="1">
<img src="captcha.php?captcha=<?php echo $i?>&time=<?php echo time();?>">
</div>
<?php endfor ?>
The css code
#captcha
{
width:400px;
margin:10px;
}
#captcha div
{
float:left;
margin:5px;
background:#ccc;
height:auto;
text-align:center;
width:50px;
}
#captcha input
{
width:1em;
display:block;
}
.captcha_message
{
color:red;
font-size:1.2em;
margin:10px;
}
You can download all files from this example from here Cat dog captcha
Good luck in finding the kitties
Share this with the world
Related
Comments
Sure, this is a neat idea.. The only problem is that the picture samples you are using are repetitive: it would only take a few dozen manual refreshes to download all the images, sort them into the respective dog or cat category, and use that for making a bot. The bot could just compare file sizes (I think all these images have slightly different sizes), or perhaps an MD5 hash of them.
Posted on 2007-11-15 21:34:39A more robust solution I believe would be to perhaps generate a new image, placing N pictures of cats and dogs in the picture randomly, sending that to the client, then asking how many of each one there are. Of course, this number would probably be less than 10, so even with that you could send enough junk to eventually get some through.
This is fun! Is there any way of making this captcha accessible to those who have trouble seeing the pictures?
Posted on 2007-11-16 08:22:14This looks really good to me. But I cannot download the zip'd code. Looks like something may be wrong with the link.
Posted on 2007-11-19 08:50:09the problem regarding accessibility to those who cant see the pictures can be solved with a button that when pressed will play a cat sound or a dog sound :)
Posted on 2007-11-22 14:43:52Make yourself heard