PHP CAPTCHA generator
CAPTCHA stands for Completely Automated Public Turing test to tell Computers and Humans Apart. The term captcha was first coined by Luis von Ahn, Manuel Blum, Nicholas J. Hooper and John Langford. The first three are from Carnegie Mellon University and last one is from IBM. We will see how easily we can produce captcha using the PHP web programming.
Lets define a class called captcha.
<?php
class captcha
{
}
?>
Now we need some variable to show and store captcha string in the webpage. So we need some variable. Lets have some variable in the captcha class.
<?php
class captcha
{
private $final_string;
private $image;
private $height;
private $width;
private $text_color;
private $line_color;
private $bgcolor;
?>
Now we have to generate the captcha string and paint it in an image, so that the bots can’t read it. We can do this thing in the constructor of the class.
function __construct()
{
session_start();
session_cache_limiter(’public’);
session_cache_expire(5);
$height=26;
$width=100;
$image=ImageCreate($width,$height);
$bgcolor=ImageColorAllocate($image,240,255,240);
ImageFill($image,0,0,$bgcolor);
$temp_string=”";
$final_string=$temp_string;
$length=rand(5,10);
for($a=0;$a<$length;$a++)
{
$t=rand(48,122);
if($t<91||$t>96)
{
$temp_string=chr($t);
$final_string.=$temp_string;
}
else $a–;
}
$_SESSION['result']=$final_string;
//setTextColor();
$r=rand(20,30);
$b=rand(20,30);
$g=rand(100,120);
$text_color=ImageColorAllocate($image,$r,$b,$g);
//createDotDotDash();
for($a=0;$a<40;$a+=2)
{
$line_color=ImageColorAllocate($image,rand(220,240),rand(220,240),rand(240,260));
imagedashedline($image,0,$a,$width,$a,$line_color);
}
ImageString($image,20,5,5,$final_string,$text_color);
ImageRectangle($image,0,0,$width-1,$height-1,$bgcolor);
header(”Content-Type: image/jpeg”);
ImageJpeg($image);
ImageDestroy($image);
}
Now come to description. session_start() is a built-in function to start session for user if not started. We defined our session cache as public. Because the cache control is public and the session will expire sometime in future. Now we defined the expire time as 5 minute using the session_cache_expire() function. $height is the height of our captcha image and $width is the width. $image is our image resource which is created by ImageCreate() function. We have a $bgcolor to hold the color of the background. A color can be allocated to a variable using imagecolorallocate() function with the RGB value. imagefill() function is used to fill the image with background color.$temp_string is for temporary use. We will keep our final captcha string in $final_string. The length of the captcha string is kept in $length. A for loop is used to generate the captcha string tokens with rand() function. The final captcha string is then kept to the session variable. The text color is defined in $text_color. Dashed lines are used inside the captcha image where the dashed lines have a range of random color.
The captcha string is written to the image using imagestring() function. The header function is used to keep the type in the image header. The drawn image is then put to ImageJpeg() function to get an output. Finally imagedestroy() function frees the memory associated with the image in the server.
Let us put the captcha.php file on captcha folder. Now we create another file checkbot.php in the root where the captcha folder exists.
<?php
require(’captcha/captcha.php’);
$a=new captcha();
?>
The require() function is not like include(), the captcha.php file is ready to be used in the checkbot.php but its code is not put here. $a is a object of captcha class.
Now the code is ready to generate captcha code for you. We need a refresh function to regenerate the captcha string. A javascript code is a better choice for this. A refresh image is needed for this too.
Just copy and paste the script in the page head.
<script type=”text/javascript”>
function refresh()
{
var ran=Math.floor(Math.random()*10);
var path=document.getElementById(”imgsrc”).src;
document.getElementById(”imgsrc”).src=path+”?rand=”+ran;
}
</script>
Variable ran contains a random number and path gets the path of a element which have an id called “imgsrc”. Our captcha image id will be “imgsrc”. And the image is redrawn by sending a new path of the image source to the document.getElementById().src which is the image source itself.
Put the following html code to generate the captcha where you want to be.
<img id=”imgsrc” src=”checkbot.php” align=”absmiddle”/><img src=”refresh.gif” onClick=”javascript:refresh();” align=”absmiddle” /><input type=”text” name=”security_code” size=”10″ align=”absmiddle” />
You can change the value of the tag attributes to customize yourself.
Download the captcha.php and checkbot.php file from here.
This is one of the simplest captcha, captcha should be hard enough to bypass, so it should not be designed insecuedly.
Tags:captcha, captcha stands for, javascript, PHP
