Create your own captcha to verify forms

Recently the forms on my company’s website had been getting lots of spam. The spammers were submitting the forms with advertisements. We needed a captcha. I suggested Recaptcha, but if you have seen one of their captcha, you would think even a human cannot interpret them correctly. So my boss did not want that. We wanted our customers to have a simple, easy to interpret captcha while also serving us some help fighting the spam. So we decided to create our own simple captcha system, with captchas our customers will not hate. Download Full Source Code Here is how it works. On an A4 sheet write a bunch of words your customers will like. Ask others to write as well so you have different kinds of handwritings. We wrote words like profit, work, tax, business, money etc, words our customers are familiar with. Then scan the A4 paper. Using an image editor, create a file for each images. give cryptic filenames so it cannot be guessed. Using ms-excel, create a csv file with two columns. first column will have the filename and second column will have the text on the image. (the answer). Now the coding.. We created a simple captcha class. The source code is self explanatory.
debug('constructor');
	$this->filepath = '';
    }

    private function debug($msg){
	 //  echo '

'.$msg.'

'; } public function setPathToCsvFile($path){ $this->debug('setPathToCsvFile'); $this->filepath = $path; } public function getRandomImage(){ $files = $this->find_all_files('forms/captcha/images'); if($files == false){ die('Error: image folder not found');} $randomNumber = rand (2 , count($files)-1); return $files[$randomNumber]; } private function find_all_files($dir){ $root = scandir($dir); foreach($root as $value) { if($value === '.' || $value === '..'||$value === 'index.html') {continue;} if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;} foreach(find_all_files("$dir/$value") as $value) { $result[]=$value; } } return $result; } public function checkCaptcha($userinput, $img_filename){ $userinputValidated = strtolower($userinput); $this->debug('checkCaptcha'.$userinputValidated.' '.$img_filename); $success = false; if($this->filepath != ''){ if($img_filename){ $solution = $this->getSolution($img_filename); $this->debug($solution); } else { die('image filename is null');} } else { die('Error: file path is null. use setPathToCsvFile method before checkCaptcha method');} if(($solution)&&($solution == $userinputValidated)){ $success = true; } return $success; } private function getSolution($img_filename){ $solution = false; $filename = basename($img_filename); if (($handle = fopen($this->filepath, "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $row++; $num = count($data); for ($c=0; $c < $num; $c++) { if($data[$c] == $filename){ $solution = $data[$c+1]; } } } fclose($handle); } return $solution; } } ?>
This is how you would use it in the form. We instantiate a captcha using the above class and call its getRandomImage method. This will give us a path which we will use to display the image in img tag. We also create a hidden field to send this path back to server.

Client Name
' />
Your Email
' />
Document
Message
Type the word in the image getRandomImage(); ?> '/> '/>
?>

Form fill up

Please fill up the following to upload your electronic file. If your file is greater than 100MB, please contact us. "; }else{ $captcha1 = new ThupCaptcha(); $captcha1->setPathToCsvFile('forms/captcha/solutions.csv'); $captchaSuccess = $captcha1->checkCaptcha($userinput_Captcha, $img_path); if(!$captchaSuccess){ $message .= "Captcha did not match."; } } if (empty($Name)) { $message .= "Enter your company name."; } if( $upload_Size == 0) { $message .= "Enter a valid file for upload."; } if( $upload_Size > 75*1024*1024) { //delete file unlink($upload_Temp); $message .= "Your file is too big. Please contact us."; } if($message != ''){ echo "

Please fix the following errors:

    " . $message . "
"; displayForm(); } else{ //process the form } ?>