Saturday, March 12, 2011


  Create Captcha/Validation Field

What is the CAPTCHA field?  What is the Validation field?

The captcha field is the wiggly letters that show up all slanted (like an image, not like text from the website) that it asks you to fill in to make sure you are not a bot or a spammer.
Sometimes it's hard to read, though, and they're nonsense words, so if it's "1 dog" and you write "I dog" accidentally, it will give you an error.
Just fill out what you see. If you don't see anything, try reloading the page or using a different browser.

Demo View

File Name : captcha.php
<?php 
    //Start session 
    session_start();
    //out put chaptcha image type
    header('Content-type: image/jpeg');
    //length you character which has captcha image
    $length = 6;
    // characters want to display in out put 
    $chars = "abcdefghijklmnopqrstuvwxyz123456789";
    $string = "";
    while (strlen($string) < $length) {
    $string .= $chars[mt_rand(0,strlen($chars))];
    }
    //if you use letters and numeric symbols, convert all to  upper case letter is more user friendly
    $string = strtoupper($string);
    //get a random generated sting to PHP session 
    $_SESSION['validation_code'] = $string;
    $im = imagecreate( 225, 50 );
    // if you want to set background image, uncomment below line and put correct image name you have 
    //$im = imagecreatefrompng('images/captcha.png');
    
    //set capthca image background color
    $bg   = imagecolorallocate($im, 255, 255, 255);
    //define colors for string 
    $grey    = imagecolorallocate($im, 128, 128, 128);
    $black    = imagecolorallocate($im, 0, 0, 0);
    $redpen    = imagecolorallocate($im, 255, 0, 0);
    $googlered   = imagecolorallocate($im, 197,18,44);
    $greenpen   = imagecolorallocate($im, 0, 153, 0);
    $greennew  = imagecolorallocate($im, 127, 255, 170);
    $googleblue = imagecolorallocate($im, 65, 103, 220);
    $bluepen   = imagecolorallocate($im, 0, 0, 255);
    $blackpen   = imagecolorallocate($im, 0, 0, 0);
    $yellowpen  = imagecolorallocate($im, 255, 200, 0);
    $aquapen   = imagecolorallocate($im, 0, 255, 255);
    $fuschiapen = imagecolorallocate($im, 255, 0, 255);
    $greypen   = imagecolorallocate($im, 153, 153, 153);
    $silverpen   = imagecolorallocate($im, 204, 204, 204);
    $tealpen   = imagecolorallocate($im, 0, 153, 153);
    $limepen   = imagecolorallocate($im, 0, 255, 0);
    $navypen   = imagecolorallocate($im, 0, 0, 153);
    $purplepen  = imagecolorallocate($im, 153, 0, 153);
    $maroonpen = imagecolorallocate($im, 153, 0, 0);
    $olivepen   = imagecolorallocate($im, 153, 153, 0);
    $textcolor    = imagecolorallocate($grey, $black, $redpen, $googlered, $greenpen, $greennew,
 $googleblue, $bluepen, $blackpen, $yellowpen, $aquapen, $fuschiapen, $greypen, $silverpen, 
$tealpen, $limepen, $navypen, $purplepen, $maroonpen, $olivepen);
    $font = 'fonts/AManExtendedBold.ttf';
    $location_x = 5;
    //randomly gets color for string 
    $font_color = $textcolor[rand(0,19)];
    for($counter=0; $counter < strlen($string) ; $counter++){
        imagettftext($im, 27,0, $location_x, 40, $font_color , $font, substr($string,$counter,1) );
        $location_x = $location_x + 35 ;
    }
            
    imagejpeg($im);
    imagedestroy($im);
    
?>
 
File Name : index.html
<html>
    <head>
<title>Your Page Title</title>
       </head> 
            <body>
            <form id="form_name" name="form_name" method="post" action="action.php">
            <table border="0" cellspacing="3" cellpadding="3">
              <tr>
                <td align="right" valign="middle">Name</td>
                <td valign="middle"> </td>
                <td colspan="2" align="left" valign="middle">
                <label><input name="txt_name" type="text" class="login_reg" id="txt_name" style="width:250px; height:13px; vertical-align:top" />
</label>
                </td>
              </tr>
              <tr>
                <td  align="right" valign="middle">Email Address</td>
                <td valign="middle"></td>
                <td colspan="2" align="left" valign="middle">
                <label><input name="txt_mail_ad" type="text" class="login_reg" id="txt_mail_ad"style="width:250px; height:13px; vertical-align:top" />
</label>
                </td>
              </tr>
              <tr>
                <td align="right" valign="middle">Phone No</td>
                <td valign="middle"> </td>
                <td align="left" valign="middle">
                <label><input name="txt_area_code" type="text" class="login_reg" id="txt_area_code" style="width:40px; height:13px; vertical-align:top" />
</label>
                </td>
                <td align="left" valign="middle">
                <label><input name="txt_rest_no" type="text" class="login_reg" id="txt_rest_no" style="width:90px; height:13px; vertical-align:top" />
</label>
                </td>
             </tr>
              <tr>
                <td align="right" valign="top" class="main"> </td>
                <td valign="top"> </td>
                <td align="left" valign="top" class="normalText">Area code</td>
                <td align="left" valign="top" class="normalText">Rest no</td>
              </tr>
              <tr>
                <td  align="right" valign="top">Short description <br />about your urgency</td>
                <td> </td>
                <td colspan="2" align="left" valign="top">
                <label>
     <textarea name="txa_des" cols="25" rows="15"  id="txa_des" style="background-color:#FFF; border:1px solid; border-color:#7ba5ce; width:300px">
     </textarea></label>
                </td>
              </tr>
              <tr>
                <td align="right" class="messagedata" >Validation Code</td>
                <td> </td>
                <td colspan="2" align="left"><img src="captcha.php" width="125" height="40" alt="Captcha Code" /></td>
                </tr>
              <tr>
                <td align="left"> </td>
                <td align="left"> </td>
                <td colspan="2" align="left">
                  <label>
                    <input type="text" name="validation_code" id="validation_code" style="width:90px; height:13px; vertical-align:top"  maxlength="6" />
                  </label>
                </td>
              </tr>
              <tr>
                <td align="left"> </td>
                <td align="left"> </td>
                <td colspan="2" align="left">
                <label><input name="btn_submit" type="submit" class="send_button_long" id="btn_submit" value=" Send Message » " />
</label>
                </td>
                </tr>
            </table>
        </form>
     </body>
</html>
         
File Name : action.php
<?php
session_start();
header("Cache-control: private");
if(trim($_SESSION['validation_code']) != trim($_REQUEST['validation_code'])){
 echo 'Error validation code';
}else{
 echo 'Vlidation code ok';
}
?>

1 comment:

  1. This is a nice article..
    Its easy to understand ..
    And this article is using to learn something about it..

    c#, dot.net, php tutorial, Ms sql server

    Thanks a lot..!
    ri70

    ReplyDelete