Skip to main content

How to Make Your Own PHP Captcha Generator

In this article we will create file based simple yet successful captcha generator.

3 Major Anti-spamming techniques used?

Mathematical Operation like Random number + Random Number = -> The user must specify the answer
Random word -> User must type the word
Random question -> Obvious one which the user should answer correctly [ex: Are you human?]

How Captcha works?

The captcha generator generates an IMAGE with the question and then put up a session variable storing the value.
User input though an input box.
Using php POST, we compare the session variable data with the user input and tell whether its a bot or human. Its coding time

The Code

First let's write the php script which generates the captcha image. We use the simple header-content change technique, from which we can easily bring up an image from a given text.
captcha.php

PHP Code:
array("Num" => "Num"),
1 => array("Are you human?" => "yes"),
2 => array("Type 'one' " => "one"),
3 => array("Type 'test' " => "test"),
4 => array("AxHGA" => "AxHGA"),
5 => array("zontek" => "zontek"),
6 => array("12terd " => "12terd")
);
//Now we need to pic up a random question, array_rand is the perfect thing to this
$r = array_rand($words);
//Then we check about what we have in the select array
switch(key($words[$r])){
//If we have the "NUM" selected, that is a special one
//Num means the user will be prompted to doa simple addition like 5+6
case "Num":
//Pretty basic stuff, generate 2 random numbers and tell the user to put the addition
$i = rand(1,10);
$j = rand(1,10);
$ans = $i+$j;
$ques = "$i + $j = ";
break;
default:
//If not a number, ask the user a question or ask him to type a word
$key = key($words[$r]);
$ques = $key;
$ans = $words[$r][$key];
break;
}

//NOW we put up the answer to the session variable
$_SESSION['cap'] = strtolower($ans);
//This would change the content type, or in english this would tell the browser that
//what ever retuened by this script is an image
header('Content-Type: image/png');

//Following code is to generate the image from the test
//We first specify colour ranges, you can refer to the php manaul for more
$img = imagecreatetruecolor(250,30);
//In the above code, the image size is set to 250x30
$white = imagecolorallocate($img,255,255,255);
$grey = imagecolorallocate($img,128,128,128);
$black = imagecolorallocate($img,0,0,0);
//Filling the rectangle with white as we need black text on white
imagefilledrectangle($img,0,0,399,29, $white);
$text = $ques;
//THE below code is CRITICAL. This is the palce where we tell which font to use.
//Choose any ttf you like and name it as font.ttf or change the following code, make sue
//you put the path to the file correctly [i used STENCIL so that parsers will find it hard to detect]
$font = "./font.ttf";
imagettftext($img,20,0,11,21,$grey,$font,$text);
imagettftext($img,20,0,10,20,$black,$font,$text);
//Creating a PNG image, i use png cuz i <3 png [really its so small and efficient ;)] imagepng($img); //And then remove the memory parts once the output is given imagedestroy($img); ?>
2. Haa that's all But you still need to know how to use the thing. We make our index.php and ask the user for the input as determine whether it is a spammer or not.

index.php

PHP Code:



Simple Captcha Script









Upload the stuff and make sure you have the following files in the same directory level : index.php captcha.php font.ttf



It's done, now you have your own little cpatcha script, well this is really basic, but once you know what to do you have no limits

Feel free to develop the code [and be kind enough to send me one as well ]

See the demo of the script here : http://expementa.freezoka.net/captcha/

Comments

Popular posts from this blog

Learn phpfox

PHPFox  is a social network script, it is an internet application and when you install it, it is a website. The  phpfox  script comes in 3 packages, each with a different set of modules. it has two products: 1. Nebula (upto phpfox 3.8) 2. Neutron (Newer) You can check the demo on :  http://www.phpfox.com =================================================== To clear cache in phpfox follow the following steps, admincp >> Tools >> Maintenance >> Cache Manager >> Click on Clear All button =================================================== To work facebook app on local Following settings need to done in facebook app   1) go => setting => Advance 2) see "OAuth Settings" area and set "Valid OAuth redirect URIs" =  http:// projectdomain /index.php?do=/user/login/, http:// projectdomain .com/index.php?do=/user/register/, http:// projectdomain .com, http:// projectdomain .com/index.php 3) en...

Joomla Flash Tutorial

Joomla Flash Tutorial Adding a custom-made Flash animation created with Anim-FX to Frontpage is a snap By following these simple steps, you will be up and running in no time at all and impressing all of your visitors with your talents! First create your Flash animations in Anim-FX, and make sure the *.swf and *.txt files are in the base html directory.  Do not make the mistake of uploading them into the images or images/flash directory.  Also the flash does not work if loaded into the template directory.  Both files must be in the html directory where the index.php file is located. Then you can either copy the HTML generated by Anim-FX into the index.php file to the appropriate location or use a Joomla Flash Module.  Edit the index.php file which drives your Joomla template. Method 1: Editing the index.php for your Joomla template: Copy the follow...