Captcha in PHP

CAPTCHA  is stand for "Completely Automated Public Turing Test To Tell Computers and Humans Apart."

A CAPTCHA is a program that can generate and grade tests that humans can pass but current computer programs cannot.

CAPTCHA  is used for 
1.Preventing Comment Spam in Blogs.

2.Protecting Website RegistrationProtecting Website Registration.

3.Online Polls. 


4.Preventing Dictionary Attacks. 


5.Search Engine Bots.  

Output:

 

Source code:

<?php

$CAPTCHA_STRENGTH=5;

$random_str = md5(microtime());

$captcha_str = substr($random_str, 0, $CAPTCHA_STRENGTH);

$width = ($CAPTCHA_STRENGTH * 10)+10;

$height = 20;

$captcha_img =ImageCreate($width, $height);

$back_color = ImageColorAllocate($captcha_img, 0, 0, 0);

$text_color = ImageColorAllocate($captcha_img, 255, 255, 255);

$line_color = ImageColorAllocate($captcha_img, 255, 0, 255);

ImageFill($captcha_img, 0, 0, $back_color);

for($i= 0;$i< $width; $i += 5)
    ImageLine($captcha_img, $i, 0, $i, 20, $line_color);

for($i = 0; $i< 20; $i += 5)
    ImageLine($captcha_img, 0, $i, $width, $i , $line_color);

ImageString($captcha_img, 5, 5, 2, $captcha_str, $text_color);

header("Content-type: image/jpeg");

ImageJPEG($captcha_img);

ImageDestroy($captcha_img);
?>

No comments:

Post a Comment