Captcha, is used to secure the user input. It safeguards a user comment or any user input from spam or from automated entry using BOTs or any programs. This ensures the entry is done by HUMAN only. It gives added security to your website in addition to keeping track of the IP address, similarity in input content and the time duration between each input. There is no way you can be 100% secure from the intruders attack, but you can always do you best to stop them.
This Captcha program which I am going to discuss below prints a random math addition or subtraction which of course is sign sensitive. The figures are displayed on the image hence is not readable by any of the program or BOT etc.
Captcha Script
Save this file as captcha.php , now make a new in the same folder and name it as mytest.php.
Now open the page mytest.php, you will see an image printed with a simple sum/addition problem and a text box to type in its answer. You can always put this script into any form and can validate its input with the one stored in the session variable $_SESSION['pin']
Understanding captcha.php
The first line starts the session so that we can store session variables later in the script. The header is sent for the content type image(make sure you do that for successful running of the script). After this there is a small logic of random numbers, the arithmetic operators which we want can be store in the array $arr. Two number are randomly chosen from the range 1 to 20, you can always change this range as per your requirement. The expression is solved and is stored in the session variable, which can be accessed later on to match the human input with our legit result.
Enjoy this simple captcha protection, it works like a charm if used as it is :)
You can always make few changes like color etc to suit your need. :)
Liked it, Bookmark Article
This Captcha program which I am going to discuss below prints a random math addition or subtraction which of course is sign sensitive. The figures are displayed on the image hence is not readable by any of the program or BOT etc.
Captcha Script
<?php
session_start();
header("Content-type: image/png");
$arr=array("+","-");
$dig1=rand(1,20);
$dig2=rand(1,20);
$i=rand(0,1);
$tx=$dig1.$arr[$i].$dig2." =";
eval("\$ses=".$dig1.$arr[$i].$dig2.";");
$_SESSION['pin']=$ses;
//creates an image here
$im=@imagecreate(100,30);
$bg=imagecolorallocate($im,255,255,230);
$txt=imagecolorallocate($im,255,0,0);
imagestring($im,5,30,5,$tx,$txt);
imagepng($im);
imagedestroy($im);
?>
Save this file as captcha.php , now make a new in the same folder and name it as mytest.php.
<?php
echo "<img src='re_cap_fun.php' alt='image' align='middle' />";
?>
<input type='text' size='2' />
Now open the page mytest.php, you will see an image printed with a simple sum/addition problem and a text box to type in its answer. You can always put this script into any form and can validate its input with the one stored in the session variable $_SESSION['pin']
Understanding captcha.php
The first line starts the session so that we can store session variables later in the script. The header is sent for the content type image(make sure you do that for successful running of the script). After this there is a small logic of random numbers, the arithmetic operators which we want can be store in the array $arr. Two number are randomly chosen from the range 1 to 20, you can always change this range as per your requirement. The expression is solved and is stored in the session variable, which can be accessed later on to match the human input with our legit result.
Enjoy this simple captcha protection, it works like a charm if used as it is :)
You can always make few changes like color etc to suit your need. :)
- Administrator says:
- on 25 December,2007 20:12hrs
Yeah ofcourse, and its simple and works like a charm.. ;)
- Saeed says:
- on 08 February,2008 15:02hrs
Nice SCript.
- Tope says:
- on 22 March,2008 11:03hrs
Sorry, but I couldn't get the script to work both on my testing server and remote hosting server. The image does not show. Please help.
- Tope says:
- on 22 March,2008 11:03hrs
Sorry, working now. loaded mytest.php instead of captcap.php
- Tope says:
- on 22 March,2008 13:03hrs
Finally got the script to work. For beginner like me there are two 2 issues to fix on mytest.php file:
1. Second line should reference captcha.php rather than re_cap_fun.php,
2. start mytest with session_start(); immediately after the php opening tag.
Share your views:
Name (required)
Email (will not be displayed)(required)
min. 10 characters
| Enter Result to prove you are human |


![Validate my RSS feed [Valid RSS]](http://thinkofcode.com/ask/img/valid-rss.png)

Excellent. "Do math to prove you aren't a computer!"