Wednesday, 15 August 2012

HOW TO write code for simple captcha in PHP

// siddhu vydyabhushana // Leave a Comment
hi friends today i posted How to upload photos .and another chance i got to show how to write code in php,
it is very easy and funny too,on writing code i got some struggles so i posted in blog in easy steps


save the below code as any name with file extension .PHP
Click here to download

<?php
session_start();

$string = '';

for ($i = 0; $i < 5; $i++) {
$string .= chr(rand(97, 122));
}

$_SESSION['random_number'] = $string;

$dir = 'fonts/';

$image = imagecreatetruecolor(165, 50);

// random number 1 or 2
$num = rand(1,2);
if($num==1)
{
$font = "Capture it 2.ttf"; // font style
}
else
{
$font = "Molot.otf";// font style
}

// random number 1 or 2
$num2 = rand(1,2);
if($num2==1)
{
$color = imagecolorallocate($image, 113, 193, 217);// color
}
else
{
$color = imagecolorallocate($image, 163, 197, 82);// color
}

$white = imagecolorallocate($image, 255, 255, 255); // background color white
imagefilledrectangle($image,0,0,399,99,$white);

imagettftext ($image, 30, 0, 10, 40, $color, $dir.$font, $_SESSION['random_number']);

header("Content-type: image/png");
imagepng($image);

?>

0 comments:

Post a Comment