MyBB Community Forums

Full Version: Dog/Cat Captcha images not showing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using this Cat-Dog Captcha on my forum and sometimes all the images will not load completely. I can change this code to 5 instead of a 6
define('CAPTCHA_NR',6); 
all of the images still won't load all the way but it occurs less times. I'm lost on what to do. My test forum link is below try it so you can see what it is doing. Keep hitting refresh. Thanks,

http://rickmick.com

the php and html code
<?php
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;
}
?>
<html>
<head>


</head>
<body>

		<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="catdogcaptcha.php?captcha=<?php echo $i?>&time=<?php echo time();?>">
			</div>
		<?php endfor ?>
		</div>
		</p>
		

</body>
</html>

The css code I add to my theme global.css


#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;
}
A member here found my problem. Thanks so much.

The reason for this is in the cats directory you are missing the file names 13.jpg and 15.jpg, therefore when the rand() function selects the numbers 13 or 15 for cats, you will see no image as it is not present on the server.
I have found another problem. When all checkboxes are unchecked it allows the post to go through.

//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
		}
	}
}