Friday 31 May 2013

how to create your own captcha image easily

This tutorial tell you how to create your own captcha image.
You should follow these steps:
  1. Copy the code below and save it as "create_image.php"
    <?php
    //Start the session so we can store what the security code actually is
    session_start();
    //Send a generated image to the browser
    Create_Captcha_Images();
    exit();
    function Create_Captcha_Images() {
    $width='135';
    $height='30';
    //Let's generate a totally random string using md5
    $md5_hash = md5(rand(0,999));
    //We don't need a 32 character long string so we trim it down to 5
    $code = substr($md5_hash, 15, 5);
    //Set the session to store the security code
    $_SESSION["security_code"] = $code;
    //create image resource using width and height
    $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
    /* set the colours */
    $background_color = imagecolorallocate($image, 255, 255, 255);
    $text_color = imagecolorallocate($image, 0, 0, 0);
    $noise_color = imagecolorallocate($image, 100, 120, 180);
    /* generate random dots in background */
    for( $i=0; $i<110; $i++ ) {
    imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
    }
    /* generate random lines in background */
    for( $i=0; $i<250; $i++ ) {
    imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
    }
    // add white string to box with params: image, font (1-5), x, y, string and color
    imagestring($image, 5, 40, 7, $code, $text_color);
    /* output captcha image to browser */
    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);

    }
    ?>
  2. Copy the code below and save it as "contact_us.php".
    <?php
    session_start();
    if(isset($_POST['submit']))
    {
    $image = $_POST['antibot_input_str'];
    if($image == $_SESSION["security_code"])
    {
    echo 'Your Entered code has matched go ahead. e.g. store your form data into database';
    }
    else
    {
    $mail_sent = 'Your Entered code does not match';
    }
    }
    ?>
    <!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=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form method="post" action="contact_us.php">
    <table align="center">
    <tr valign="middle">
    <td class="FormButton">First name</td>
    <td><font class="Star">*</font></td>
    <td nowrap="nowrap">
    <input type="text" value="" maxlength="32" size="32" name="firstname" id="firstname">
    </td>
    </tr>
    <tr valign="middle">
    <td class="FormButton">Last name</td>
    <td><font class="Star">*</font></td>
    <td nowrap="nowrap">
    <input type="text" value="" maxlength="32" size="32" name="lastname" id="lastname">
    </td>
    </tr>
    <tr>
    <td class="iv-box-descr" colspan="3">Type the characters you see in the picture. (If you do not see any picture here, please enable images in your web browser options and refresh this page):</td>
    </tr>
    <tr>
    <td class="iv-box" colspan="2">
    <div class="iv-img">
    <img id="imgCaptcha" src="create_image.php"/><br>
    <a onclick="
    document.getElementById('imgCaptcha').src = 'create_image.php?' + Math.random();
    return false;
    " href="javascript:void(0);">Get a different code</a>
    </div>
    <br> </td>
    <td class="iv-box">
    <input type="text" name="antibot_input_str">
    </td>
    </tr>
    <tr valign="middle">
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>
    <input id="insert" name="submit" type="submit" value="Submit" />
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

0 comments:

Post a Comment