Posted in: PHP

Captcha Code PHP Example Script | Secure Form PHP

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.

One Phrase for getting the random text.

Step 2. Generate Random Text

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

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.

Above code will create an image with the Constants values.

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

$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.

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().

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().

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.

Step 8. Showing Image on Page

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

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

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

Hope you like the Captcha Code PHP Example Script

~~~~ Happy Codeing ~~~~

Comments (4) on "Captcha Code PHP Example Script | Secure Form PHP"

    1. 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

Leave a Reply

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