again php captcha script
You’ve already seen a post about captcha.If not i encourage you to see it.Thuough it is pretty simple captcha script.If you try to run that script you will notice that it is not like others where they have different type of background color,different type of font,noise.So in this post i will try to figure that.
First make a file name “captcha.php“
now paste
<?php
session_start();
function captcha($length) {
// generate a totally random string using md5
$rd = md5(rand(0, 999));
// We don't need a 32 character long string so we trim it down to 5 because md5 will return a long string
$code = substr($rd, 15, $length);
// Set the session to store the security code
$_SESSION["captcha"] = $code;
// Set the image width and height
$width = 200;
$height = 50;
// Create the image resource
$image = imagecreate($width, $height);
// Random RGB colours
$rgb1 = rand(0, 255);
$rgb2 = rand(0, 255);
$rgb3 = rand(0, 255);
$background = imagecolorallocate($image, $rgb1, $rgb2, $rgb3);
$text = imagecolorallocate($image, ($rgb1 - 60), ($rgb2 - 60), ($rgb3 - 60));
// Make the background colour
imagefill($image, 0, 0, $background);
for ($i = 0; $i < 10; $i++) {
$rx1 = rand(0,$width);
$rx2 = rand(0,$width);
$ry1 = rand(0,$height);
$ry2 = rand(0,$height);
$rcVal = rand(0,255);
$rc1 = imagecolorallocate($image,
rand(0,255),
rand(0,255),
rand(100,255));
//giving noise
imageline ($image, $rx1, $ry1, $rx2, $ry2, $rc1);
}
// Add randomly generated string in the image
for($i = 1; $i <= $length; $i++) {
$counter = rand(1, 2);
if ($counter == 1) {
$angle = rand(0, 45);
}
if ($counter == 2) {
$angle = rand(315, 360);
}
imagettftext($image, rand(14, 20), $angle, ($i * 25), 30, $text, "font/LucidaBrightDemiItalic.ttf", substr($code, ($i - 1), 1));
}
// Add a border
imagerectangle($image, 0, 0, $width - 1, $height - 1, $text);
// Tell the browser what kind of file is come in
header("Content-Type: image/png");
// Output the newly created image in jpeg format
imagepng($image);
// Free up resources
imagedestroy($image);
}
?>
this code into that file.
You see “LucidaBrightDemiItalic.ttf”.I have this font in font directory.You can use ur font.And that will be your font style of captcha.But remember you have to put font file in the directory or Sub-directory where your php file will stay.
Now include this file in any other php file and make a call with
captcha(4);
here 4 is the number of character you want to show.You can give your length of string.Here we use session to store the captcha string in $_SESSION global array.Since we have to use this later, to check whether the user submit the data or not.So that’s it.
And “Have a good programming“






last comment