Simple PHP Captcha Image Verification or Validation
Hello guys, This is yet another tutorial for creating captcha Image Verification or Validation using simple php script.
And here goes the script
The below script is captcha.php
<?php
session_start();
$strlength = rand(4,7);
function createRandomString() {
$chars = “abcdefghijkmnopqrstuvwxyz023456789″;
srand((double)microtime()*1000000);
$i = 0;
$pass = ” ;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$captchastr = strtoupper(createRandomString());
$randcolR = rand(100,230);
$randcolG = rand(100,230);
$randcolB = rand(100,230);
//initialize image $captcha is handle dimensions 200,50
$captcha = imageCreate(200,50);
$backcolor = imageColorAllocate($captcha, $randcolR, $randcolG, $randcolB);
$txtcolor = imageColorAllocate($captcha, ($randcolR - 30), ($randcolG - 30), ($randcolB - 30));
for($i=1;$i<=$strlength;$i++)
{
$clockorcounter = rand(1,2);
if ($clockorcounter == 1)
{
$rotangle = rand(0,45);
}
if ($clockorcounter == 2)
{
$rotangle = rand(315,360);
}
//$i*25 spaces the characters 25 pixels apart
imagettftext($captcha,rand(14,20),$rotangle,($i*25),30,$txtcolor,”/arial.ttf”,substr($captchastr,($i-1),1));
}
for($i=1; $i<=4;$i++)
{
imageellipse($captcha,rand(1,200),rand(1,50),rand(50,100),rand(12,25),$txtcolor);
}
for($i=1; $i<=4;$i++)
{
imageellipse($captcha,rand(1,200),rand(1,50),rand(50,100),rand(12,25),$backcolor);
}
//Send the headers (at last possible time)
header(’Content-type: image/png’);
//Output the image as a PNG
imagePNG($captcha);
//Delete the image from memory
imageDestroy($captcha);
$_SESSION[captchastr] = $captchastr;
?>
How to add this on your form ?
just add this HTML code <img src=”captcha.php”><input type=”text“ name=”captcha“>
Just keep the font in the same path where the captcha.php is placed, download the FONT arial.ttf
Thats all simple thanks guys.
Related posts:
