Like Us on Facebook

How to Create a Captcha Code using PHP

In this tutorial I will explain how to create a Captcha using PHP. We are using some of the features available in PHP for creating an image. This is very simple, basic, easy, tutorial. And we all know very well that captcha code mainly used to avoid spam or auto-submission. So lets start..


 

The script contains three  PHP files.

index.php // Home page form

submitPage.php // Captcha code checking

captcha_code_file.php // Captcha Creation
 

index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Captcha Demo - UandBlog</title>
</head>


<body style="background-color:#D8D8D8;">

<div style="width:500px; margin:0 auto; margin-top:100px; background:#F0F0F0; padding:10px;">

<h2>Captcha Demo - UandBlog</h2>


<form action="submitPage.php" method="post">
    <table width="100%" border="0" cellspacing="5" cellpadding="5">
      <tr style="color:#F00;">
             <td colspan="2" style="font-family:Arial, Helvetica, sans-serif; font-size:15px;">
              <?php if(isset($_REQUEST['msg1'])){?>
              <?php echo $_REQUEST['msg1'];?>
              <?php } ?>

            </td>
                    </tr>
      <tr>
        <td>Name:</td>
        <td><input name="name" type="text" required></td>
      </tr>
      <tr>
        <td>Email:</td>
        <td><input name="email" type="email"></td>
      </tr>
      <tr>
        <td>Message:</td>
        <td><textarea name="msg" cols="" rows=""></textarea></td>
      </tr>
      
      <tr>
        <td style=" text-transform:uppercase;" valign="top">captcha:</td>
        
        <td>*Please fill in the captcha security below</td>
      </tr>
      
       <tr>
        
        <td valign="top">&nbsp;</td>
        <td ><input id="6_letters_code_1" name="6_letters_code_1" required type="text" style="border:1px solid #e2d5ca !important;"></td>
      </tr>
                  
      <tr>
    
        <td class="blk"  valign="top">&nbsp;</td>
        <td>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
            

              <tr>
                <td style="text-align:left;"> <img src="captcha_code_file.php?rand=<?php echo rand();?>" id='captchaimg' width="40%"></td>
                <td style="text-align:right;"></td>
              </tr>
              <tr>
              
                   <td colspan="2" style="font-family:Arial, Helvetica, sans-serif; font-size:11px;"> Can't read the image? click <a href='javascript: refreshCaptcha();' style="color:#F00;">here</a> to refresh</td>
              
              </tr>
            </table>

        </td>
      </tr>
      <tr>
              <td></td>
            <td><button class="submit_btn" name="Submit1" type="submit">Submit</button></td>
      
      </tr>
    </table>
</form>

</div>


    <script type='text/javascript'>
        function refreshCaptcha()
        {
            var img = document.images['captchaimg'];
            img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
        }

    </script>
    
</body>

</html>


submitPage.php
<?php
//UandBlog Demo

session_start();
if(isset($_REQUEST['Submit1'])){
    // code for check server side validation
    if(empty($_SESSION['6_letters_code_1'] ) ||
        strcasecmp($_SESSION['6_letters_code_1'], $_POST['6_letters_code_1']) != 0)
    {  
        $msg1="The Validation code does not match! Please Select Your Option Again";
        
        ?>
        
        <script>
                window.location.href='index.php?msg1=<?php echo $msg1;?>';
        </script>


<?php 
}else{
$msg1="The Validation code matched !";
// DB Code
?>

        <script>
                window.location.href='index.php?msg1=<?php echo $msg1;?>';
        </script>

<?php
}
}
?>

captcha_code_file.php
<?php 
session_start();
//Settings: You can customize the captcha here
$image_width = 200;
$image_height = 60;
$characters_on_image = 5;
$font = './monofont.ttf';

//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)

$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 140;
$random_lines = 5;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";

$code = '';


$i = 0;
while ($i < $characters_on_image) { 
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}


$font_size = $image_height * 0.75;
$image = @imagecreate($image_width, $image_height);


/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);

$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'], 
        $arr_text_color['green'], $arr_text_color['blue']);

$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'], 
        $arr_noice_color['green'], $arr_noice_color['blue']);


/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
 mt_rand(0,$image_height), 2, 3, $image_noise_color);
}


/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
 mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}


/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code); 
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);


/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
$_SESSION['6_letters_code_1'] = $code;

function hexrgb ($hexstr)
{
  $int = hexdec($hexstr);

  return array("red" => 0xFF & ($int >> 0x10),
               "green" => 0xFF & ($int >> 0x8),
               "blue" => 0xFF & $int);
}

?>

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.