Friday, August 26, 2011

Develop a Simple Anti spam Captcha in PHP with Custom Font


When working with web forms on web development.  Preventing spam is important process. But most of the developers are feel Captcha is very big process, nothing like that.
Here is the very simple Captcha developed in PHP with custom font rendering option. You can change the look and feel based on your project need.

Let us see how to create this Capcha in PHP:


Now I am going to generate a 4 Digit Random number and than store into session variable using ($_SESSION). Based on the 4 Digit Random number I will generate a simple capcha with the custom font feedbackbb.ttf.





capcha_image.php
<?php session_start();
$random_number=rand(1234,9999);
$_SESSION['capcha_code']=$random_number;
header("Content-type: image/png");

$image = imagecreate( 100, 25 );  // Capcha Width Height
$bgcolor = imagecolorallocate($image, 206,174,148); // Create Background Fill
$linecolor = imagecolorallocate($image, 0,0,0 ); // Set Line Color RGB
$textcolor = imagecolorallocate($image, 0,0,0 ); // Set Text Color RGB
$font="feedbackbb.ttf"; // Load font file

for($i=0;$i<13;$i++)
imageline( $image, ($i*20),0, 0, ($i*20), $linecolor );	

imagettftext($image, 15,0,10,20, $textcolor, $font,$random_number );
imagepng($image);
?>
example_view.php
<img src="capcha_image.php" alt="Verify" width="75" height="25" /> 
 
View Example Newsletter Script with this Capcha 

No comments:

Post a Comment