32,000+ SUBSCRIBERS CAN'T GO WRONG !!!

phpans
GET TOP ARTICLES, SNIPPETS, SCRIPTS IN YOUR INBOX FOR FREE!

Delivered by GET EMAIL UPDATES FeedBurner
We always respect your privacy and take protecting it seriously.
We won't Spam You

Human Readable Image Captcha In PHP

, 0,
Human Readable Image Captcha In PHP
In this article, I am going to present Human Readable Image Captcha In PHP. We already know that captcha is a computer program using on input for verification the input isn't by a robot and input submitted by a human. Before we discussed on Create captcha in PHP and today our task is human readable image captcha in php.
In this captcha system, we will display some random words in captcha image. Before we used a random code for captcha. So, the program displayed random code in an image. Let's start creating this program now.

How To Create Image Capcha


Captcha program will display code in image so we choose to set header content type as image/png and write down this header,
header("Content-type: image/png");

we will use imagepng() function for creating an image. We need a font for text display style. We selected comic.TTF for display font style. We select some words for dispplay words ,
$my_array = array("dog","cat","sheep","sun","sky","red","ball","happy","ice","green","blue","music","movies","radio","green","turbo","mouse","computer","paper,water","fire","storm","chicken","boot","freedom","white","nice","player","small","eyes","path","kid","box","black","flower","ping","pong","smile","coffee","colors","rainbow","plus","king","tv","ring","chief","dark","envious","consist","passenger","try","peace","tangible","saw","petite","different","amuck","ship","deceive","massive","complete","towering","loutish","squirrel","count","employ","mark","accurate","dam","drag","productive","kindhearted","sin","moldy","feeble","hate","dog","post","red","file","start","mellow","spiritual","wrap","sun","volleyball","dry","shave","sneeze","grape","pretty","cart","type","talk","marble","clip","oil","measure","rifle","star","suggest","snow","permit","challenge","squealing","breath","hop","tire","shock","cherry","erect","spotted","tickle","corn","befitting","thankful","space","metal","wanting","young","afterthought","meaty","protest","plough","page","sock","force","silly","mouth","nine","standing","suggestion","deep","station","wilderness","smiling","kill","obtain","invincible","eight","fascinated","level","excellent","sparkling","smash");


These code will display in captcha image randomly. we use array_rand() option for get random words from this array. Here is full code of image captcha,
<?php
@session_start();
header("Content-type: image/png");
$my_array = array("dog","cat","sheep","sun","sky","red","ball","happy","ice","green","blue","music","movies","radio","green","turbo","mouse","computer","paper,water","fire","storm","chicken","boot","freedom","white","nice","player","small","eyes","path","kid","box","black","flower","ping","pong","smile","coffee","colors","rainbow","plus","king","tv","ring","chief","dark","envious","consist","passenger","try","peace","tangible","saw","petite","different","amuck","ship","deceive","massive","complete","towering","loutish","squirrel","count","employ","mark","accurate","dam","drag","productive","kindhearted","sin","moldy","feeble","hate","dog","post","red","file","start","mellow","spiritual","wrap","sun","volleyball","dry","shave","sneeze","grape","pretty","cart","type","talk","marble","clip","oil","measure","rifle","star","suggest","snow","permit","challenge","squealing","breath","hop","tire","shock","cherry","erect","spotted","tickle","corn","befitting","thankful","space","metal","wanting","young","afterthought","meaty","protest","plough","page","sock","force","silly","mouth","nine","standing","suggestion","deep","station","wilderness","smiling","kill","obtain","invincible","eight","fascinated","level","excellent","sparkling","smash");
$random_key1 = array_rand($my_array);
$random_key2 = array_rand($my_array);
$word= $my_array[$random_key1].' '.$my_array[$random_key2];
$_SESSION["captcha"] = $word;
$im = imagecreate(250, 30);
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 0);
$text = $_SESSION["captcha"];
$font = 'comic.TTF';
imagettftext($im, 15, 0, 25, 20, $text_color, $font, $text);
imagepng($im);
imagedestroy($im);
?>

We used php session for validate capthca and we will validate it. Let's go and know how to generate capthca.

How To Generate Captcha


Create a PHP file name as captcha.php and write down avobe code and we used $font = 'comic.TTF'; you should remember font comic.TTF is exists in captcha directory. Otherwise this script will fail to generate captcha code. When this script run then this script will set two words in a session name as $_SESSION["captcha"]; and diplay those words into image.

How To Validate A Form Using Captcha


If you want to learn how to validate a form by using this cpatcha then follow these steps, first create a PHP file name as index.php and write down this code,
<?php
@session_start();
if (isset($_POST['message']))
{
if ($_SESSION['captcha']!=$_POST['captcha'])
{
die('Invalid captcha code');
}

// Others functions will go here.
echo 'captcha passed';

}
else
{
echo '<form method="post">';
echo 'Your message: <input name="message" type="text"><br/>';
echo '<img src="captcha.php" alt="captcha"><br/>';
echo 'Input captcha: <input name="captcha" type="text"><br/>';
echo '<input type="submit" value="submit">';
echo '</form>';
}

You should put captcha file in this running host where you will use it. Using <img src="captcha.php" alt="captcha"> captcha will generate and display. Users will get an input box for type captcha text. When user will submit then the script will check this capthca. If submitted captcha will wrong then script will close otherwise script will do next job.

I hope you can understand the process of captcha and how it works. If you have any question about this Human Readable Image Captcha In PHP article please don't be hesitated, ask your questions and share your suggestions in comment section bellow. If you give any valuable suggestion then it will be appreciated. Start using Human Readable Image Captcha In PHP and protect your website from harmful robots and spiders.

Download PDF
Signup & Get $25 Free
Is this article helpful for you?





captcha

  • Connect
  •       
Top
Create an free account and start learning & sharing! Sign Up