Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Creating your own CAPTCHA

To create your own CAPTCHA you need to create file that will generate CAPTCHAs image, let's call it img.php. Then put this content inside it:

<?php
    session_start();
    // function to create random pixels
    function imageset9pixel($image,$x,$y,$color){
        for($i=$x-1;$i<$x+1;$i++){
            for($j=$y-1;$j<$y+1;$j++)
                imagesetpixel($image,$i,$j,$color);
        }
    }
    // create randmo string
    $text = md5(uniqid(rand(), true));
    $text = substr($text, 0, 5);

    //Set the session to store the security code
    $_SESSION["key"] = md5(strtoupper($text));
    //create image
    $image=imagecreate(150, 34);
    $width = 150;
    $height = 34;
    $bgcolor=ImageColorAllocate($image, 0, 0, 0); 
    $textcolor=ImageColorAllocate($image, 255, 255, 255); 
    $grey = ImageColorAllocate($image, 204, 204, 204); 
    //set font or load your own using $font = imageloadfont('./fontname.gdf');
    $font = 5;
    $fonth=imagefontheight($font);
    $fontw=imagefontwidth($font);
    //add text, lines and other stuff
    imagestring($image, $font, $x=150/2-$fontw*strlen($text)/2 , 
                $y=34/2-$fonth/2, $text, $textcolor);
    imageline($image, $x, rand($y,$y+$fonth), $x+$fontw*6, 
                rand($y,$y+$fonth), $textcolor);
    imageline($image, $x, rand($y,$y+$fonth), $x+$fontw*6, 
                rand($y,$y+$fonth), $bgcolor);
    ImageRectangle($image,0,0,$width-1,$height-1,$grey); 
    imageline($image, 0, $height/2, $width, $height/2, $grey); 
    imageline($image, $width/2, 0, $width/2, $height, $grey); 
    //add random pixels
    for($i=0; $i<2; $i++)
        imageset9pixel($image, rand($x, $x+$fontw*6), 
                        rand($y, $y+$fonth), $bgcolor);
    for($i=0; $i<10; $i++)
        imageset9pixel($image, rand(0, 100), rand(0,25), $textcolor);
    //Output the newly created image in jpeg format 
    header("Content-type: image/jpeg");
    imagejpeg($image); 
    
    //Free up resources
    ImageDestroy($image); 
?>

After that all you have to do to output your CAPTCHA is to include this file as image in your form and add input field like this:

<form method='post' action=''>
<p><img src='img.php'/></p>
<p><input type='text' name='captcha' style='text-transform:uppercase;'/></p>
<p><input type='submit' value='Do it'/></p>
</form>

And then check if CAPTCHA was correctly entered:

<?php
session_start();
if(isset($_POST['captcha']))
{
    //checking if session still exists
    if(isset($_SESSION['key']))
    {
        //checking CAPTCHA
        if(md5(strtoupper($_POST['captcha']) == $_SESSION['key'])
        {
            echo "<p>CAPTCHA entered correclty</p>";
        }
        else
        {
            echo "<p>Wrong captcha, you are bot</p>";
        }
    }
    else
    {
        echo "<p>CAPTCHA expired try one mor etime</p>";
    }
}
?>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus