GumCha simple captcha, no mysql no sessions, flatfile captch

Started on the old forum in June 2010 · view the original in the archive · old links redirect here

#captcha

5 replies · 1,592 views · started 16 years ago · 1 min read

4 participants
GumSlone Administrator Principal · 1,690 posts #1

here is a captcha code which i have developed for wap sites.

  1. create a new file image.php and put the next code code to it:
<?php

@exec("find ./gumcha/ -maxdepth 1 -type f -mtime +1 -exec rm -f {} \;");

$code = rand_string(5);
file_put_contents('./gumcha/'.$code, $code);

function rand_string( $length ) {
	$chars = "abcdefghijklmnopqrstuvwxyz123456789";	

	$size = strlen( $chars );
	for( $i = 0; $i < $length; $i++ ) {
		$str .= $chars[ rand( 0, $size - 1 ) ];
	}

	return $str;
}

/* YOU CAN MODIFY THE IMAGE CODE FROM HERE TO CREATE MORE COOL CAPTCHA IMAGES, eg. with different backgrounds */
header("Content-type: image/gif");
$im = @imagecreate(60, 25)
    or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 3, 5, 5,  $code, $text_color);
imagegif($im);
imagedestroy($im);

?>

create folder gumcha where the captcha image script is and chmod 777 it.

and then use this peace of code which will check if the gumcha generated code is correct:

if($_POST){
            $gumcha_code = $_POST['code'];

	if(gumcha_check($gumcha_code) == true)
	{

                       // HERE GOES THE CONTENT AFTER THE CAPTCHA VALIDATION

	}else echo 'WRONG CAPTCHA CODE';
}

function gumcha_check($code)
{
	$code = trim($code);
	$code = str_replace('.','',$code);
	$code = strtolower($code);
	if (preg_match("/[^\da-z]+/", $code)) return false;
	elseif(strlen($code)<2)return false;
	elseif(file_exists('./gumcha/'.$code))
	{
		@unlink('./gumcha/'.$code);
		return true;
	}
	else return false;
}

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Sora101 Newbie · 5 posts #2

Nice, but what if someone evil would pass $_POST['code']= "../index.php"? this is kinda dangerous.... it would be better to generate a numeric random code and use something like @unlink('./gumcha/'.intval($gumcha_code)); 🙂

I hope i explained myself

Cheers

GumSlone Administrator Principal · 1,690 posts #3

Sora101 wrote: Nice, but what if someone evil would pass $_POST['code']= "../index.php"? this is kinda dangerous.... it would be better to generate a numeric random code and use something like @unlink('./gumcha/'.intval($gumcha_code)); 🙂

I hope i explained myself

Cheers

$gumcha_code = str_replace('.','',$gumcha_code);

will do the thing

or

if (preg_match("/[^\da-zA-Z]+/", $gumcha_code)))
			$error = 'Wrong code.';

code updated

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close