Captcha Code PHP Example Script | Secure Form PHP

By | September 11, 2013

Captcha Code php example

Captcha code PHP example

Today i am going to Explain about making the Captcha Code PHP. This is the step by step tutorial for making the captcha code for your project. You can use the script in your project.

In this tutorial we will use the GD Library of PHP. So check out your PHP and get assured that your GD library is enabled in PHP.

In captcha code php example tutorial we will not use any HTML form we will just provide you the running Captcha code.

Step 1. Define some constants.

define('CAPTCHA_WIDTH', 100);
define('CAPTCHA_HEIGHT', 30);
define('CAPTCHA_NUMCHARS',7);

One Phrase for getting the random text.

pass_phrase="";

Step 2. Generate Random Text

Now we will generate the random text and save it in the pass_phrase variable.

for($i=0;$i< CAPTCHA_NUMCHARS;$i++)
{
$pass_phrase.=chr(rand(97,122));
}

In above code rand() is used for getting the random values from 97 to 122. It will loop till the Captcha_Numbers constant that is 7 so it will give us the 7 numbers. After getting the Numbers chr() function will change the number into character. As you can see we are taking 97 to 122 these are alphabets in ASCII. and chr() change the ASCII value to character. So after the whole loop process we will have the 7 random characters.

Step 4. Create the Image.

$img=  imagecreatetruecolor(CAPTCHA_WIDTH, CAPTCHA_HEIGHT);

Above code will create an image with the Constants values.

Step 3. Set color variables for Background, text, and graphic color


$bgcolor=imagecolorallocate($img, 255, 255, 255);
$textcolor=imagecolorallocate($img, 0, 0, 0);
$graphiccolor=imagecolorallocate($img, 64, 64, 64);
imagefilledrectangle($img, 0, 0,CAPTCHA_WIDTH, CAPTCHA_HEIGHT, $bgcolor);

$bgcolor – Will set the image background.

$textcolor- Will set the text color above the image.

$graphic color- Will set the graphic color for making the Captcha more strong.

In last line we will create a rectangle and set our $img to it. With constant values and background color.

Now we have the text, background color, text color, graphic color and rectangle but still we are missing the font. So we will use a font for it. Just copy and paste a font file in your project and link it to the $font.

$font = dirname(__FILE__) . '/arialn.ttf';

Here i am using the arial narrow fornt.

Step 5. Making random lines for making Captcha strong.

For making some random lines and attaching these lines to our image we will use the imageline().

for($i=1;$i< 5;$i++)
{
imageline($img,0, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphiccolor);
}

In the above loop we will create 5 random lines according to the captcha height and width and at last we will color these lines from our graphic color.

Step 6. Making Dots for more strong Captcha.

For making some random Dots and attaching these dots to our image we will use the imagepixel().

for($i=1;$i< 50;$i++)
{
imagesetpixel($img, rand() % CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphiccolor);
}

In the above loop we will create 50 random Dots according to the captcha height and width and at last we will color these dots from our graphic color. Same as lines.

Now we have our image In a rectangle with random text and some random lines and some random dots.

Step 7. Joining image with Text

Now we will add the random text to the image.

imagettftext($img, 24, 0, 5, CAPTCHA_HEIGHT-5, $textcolor, $font, $pass_phrase);

Step 8. Showing Image on Page

Now we done our image working and we are ready to show our image.

imagepng($img);
imagedestroy($img);

Now you are thinking that why we are using the destroyimage().

After using the imagepng it will create an image and save it in your temp. and after many attempts your temp will be full so we will first show the image using imagepng() over your page and after showing it it will de destroyed. So we will create it show it and destroy it in backgroung if you refresh the page you will get new Image.

So eager to test it. Go and run this code. aahhhh…. what it is showing some random numbers and alphabets and some special characters. Not problem right now our page is set to show the code as text now we will change it to the Image. By fixing the header();

Step 9. Set Content Header type

header("Content-type:image/png");

But use it before the imagepng().

header("Content-type:image/png");
imagepng($img);
imagedestroy($img);.

So run it again and you will see your Captcha code. Thank you for reading the Captcha Code php example tutorial and using it. Hope to see you again if you have any queries regarding the tutorial feel free to ask.

Full Captcha code php


define('CAPTCHA_WIDTH', 100);
define('CAPTCHA_HEIGHT', 30);
define('CAPTCHA_NUMCHARS',7);
$pass_phrase="";
for($i=0;$i< CAPTCHA_NUMCHARS;$i++)
{
$pass_phrase.=chr(rand(97,122));
}
$img= imagecreatetruecolor(CAPTCHA_WIDTH, CAPTCHA_HEIGHT);
$bgcolor=imagecolorallocate($img, 255, 255, 255);
$textcolor=imagecolorallocate($img, 0, 0, 0);
$graphiccolor=imagecolorallocate($img, 64, 64, 64);
imagefilledrectangle($img, 0, 0,CAPTCHA_WIDTH, CAPTCHA_HEIGHT, $bgcolor);
$font = dirname(__FILE__) . '/arialn.ttf';
for($i=1;$i< 5;$i++)
{
imageline($img,0, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphiccolor);
}
for($i=1;$i< 50;$i++)
{
imagesetpixel($img, rand() % CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphiccolor);
}
imagettftext($img, 24, 0, 5, CAPTCHA_HEIGHT-5, $textcolor, $font, $pass_phrase);
header("Content-type:image/png");
imagepng($img);
imagedestroy($img);

Hope you like the Captcha Code PHP Example Script

~~~~ Happy Codeing ~~~~

Rate it :)

Share with your friends

    4 thoughts on “Captcha Code PHP Example Script | Secure Form PHP

    1. Pingback: Create Thumbnail Image Dynamically Using PHP

    2. zanyar

      hi please tell me how can i make archive for my webfor example:jun 1998july 1998……agust 1997

      Reply
      1. Vivek Moyal Post author

        If you are asking for wordpress than there is a widget which will do it for you. Else you have to create the logic for your content which is coming from your DB

        Reply
    3. Pingback: Create Thumbnail PHP Script

    Leave a Reply

    Your email address will not be published. Required fields are marked *