Web Development Blog

How to create CAPTCHA using PHP

Edit: Updated code is posted here: http://superiorwebsys.com/92-make-php-captcha-more-secure/

In last article I explained what CAPTCHA is and why it is used (you can find more information about CAPTCHA here). Here I will provide PHP code that crates and uses CAPTCHA..

Here is sample output of this code:
CAPTCHA
For this you will need 2 files:
LHANDW.TTF – file with the font (click here to download)
secureImage.php – PHP code that creates and displays and image
Here is source of secureImage.php:

<?
session_start
();
header("Content-type: image/png"
);
$_SESSION["secureNumber"]=""
;
//Size of the image Width, Height
$im imagecreate(10520
);        
//Set background color
imagecolorallocate($im167218239
); 
$grey imagecolorallocate($im128128128
);
$black imagecolorallocate($im000
);        
// You can replace fornt by your own
$font 'LHANDW.TTF'
;        
for(
$i=0;$i<=5;$i
++) {
    
$numb=rand(09
);
    
$_SESSION["secureNumber"].=$numb
;
    
$angle=rand(-2525
);
    
imagettftext($im9$angle8+15*$i13$black$font$numb
);    
    
// Add shadow to the text    
    
imagettftext($im9$angle9+15*$i14$grey$font$numb
);    
}
imagepng($im
);
imagedestroy($im
);
?>

How to use this code

1. Display CAPTCHA and input field

<table border="0" cellpadding="0" cellspacing="0">
<tr>
    <td valign="top"><input name="secretNumber"  value="" maxlength="6" /></td>
    <td valign="bottom">
        <img src="/blog/posts/55/secureImage.php" alt="CAPTCHA" />
    </td>
</tr>
</table>

2. Check if correct number was entered

<?
if(!empty($_POST["secretNumber"]) &&
    
$_SESSION["secureNumber"]==$_POST["secretNumber"])
{
    
//Correct number is entered
}
{
    
//Display error message
}
?>

Using CAPTCHA will help to solve refresh problem (You can read about refresh problem here)

In following posts I will provide C# code for CAPTCHA.

Michael Pankratov

Associated tags:  PHP, Code, Security, CAPTCHA

Comments:

Rays wrote on November 5, 2009 at 08:04
Nice code! Thanks for sharing. But where should I place the php code that checks if the correct number was entered?

Michael Pankratovs wrote on November 5, 2009 at 10:47

You should place PHP code that checks if numbers are correct on top of the page that is entered in form action.


For example your form in file form.php has following line:



<form action="/someFile.php" name="searchForm" method="post">


Then on top of someFile.php you should place code that checks the numbers. If number is incorrect you redirect back to form.php.


If you ask a question, please leave your email and I will email you one I have answered your question.



Michael Pankratovs wrote on November 5, 2009 at 14:18

If you have more technical question you can go to our forum and ask there, our developers will answer all related questions. Forum just created, so don’t be scared if you will not see many topics there. Click here to see forum



Tupseammeces wrote on February 5, 2010 at 18:41
Hi my first comment whoopee...

Gooners wrote on February 26, 2010 at 05:15
Ola, what`s up amigos? :) I will be happy to get some help at the beginning. Thanks in advance and good luck! :)

ScooftUncestes wrote on March 2, 2010 at 11:04
Hi There i`ve just joined here and wanted to say hi to all of you!I really hope to give something back to this board... Cheers

Snireerets wrote on March 11, 2010 at 15:50
thanks for adding me to the forum.

Lebront8s wrote on March 13, 2010 at 15:31
Hello there people, I just registered on this amazing discussion board and desired to say hey! Have a fabulous day!

Snireerets wrote on March 17, 2010 at 19:39
Enjoying reading the posts here, thanks.

Johnzzs wrote on March 31, 2010 at 10:20
Hello, Just a quick hello from as I`m new to the board. I`ve seen some interesting comments so far. To be honest I`m new to forums in general :) John

Free Sampless wrote on April 9, 2010 at 10:41
This have to be the best site ever.

Spanish Johns wrote on April 10, 2010 at 01:32
hi everybody Just saying hello while I read through the posts hopefully this is just what im looking for looks like i have a lot to read.

Ava KINGs wrote on May 22, 2010 at 11:51
Really nice story u got here. It`d be really cool to read more about that topic. Thank you for posting such material.

Jamess wrote on June 2, 2010 at 02:22
Thans for the nice post.

MBTs wrote on July 9, 2010 at 17:21
Thanks for posting! I really enjoyed the post. I`ve already bookmark this article.

Bens wrote on July 27, 2010 at 12:15
Great article Thank you so much!

Petes wrote on July 28, 2010 at 14:00
Thanks for posting!

Johnys wrote on July 29, 2010 at 09:25
thanks for this great post

Joes wrote on July 30, 2010 at 02:37
good share, great article, very usefull for us...thanks!

Changs wrote on August 11, 2010 at 07:15
great information you write it very clean. I am very lucky to get this tips from you.

Teas wrote on August 12, 2010 at 23:21
this was my first time that i have visited you article and it is awesome ..

Gamers wrote on August 13, 2010 at 22:23
I fully agree with author opinion.

Lones wrote on August 14, 2010 at 21:21
you did a great job. The info is greatly appreciated

Wines wrote on August 15, 2010 at 00:48
it was very interesting to read.

Kirbys wrote on August 15, 2010 at 14:41
Wow,so niceeee!! Thanks for sharing!

Jonathans wrote on August 15, 2010 at 22:56
Powerful post.

Bobs wrote on October 31, 2010 at 12:48
Great Blog. I add this Post to my bookmarks.

doras wrote on November 2, 2010 at 11:07
Hello! Your site looks very professional, congrats!

Zazas wrote on November 7, 2010 at 01:20
Hello Everyone.I’m new to www.superiorwebsys.com, nice site!

Laits wrote on November 23, 2010 at 11:20
You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material

Unios wrote on November 28, 2010 at 13:46
You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material

SaiweeliaCaces wrote on January 12, 2011 at 16:23
This is my first time I have visited this site. I found a lot of interesting information in your blog. From the tons of comments on your articles, I guess I am not the only one! keep up the impressive work.

Preas wrote on February 26, 2011 at 12:01
hi, good site very much appreciatted

Spepleenfoxs wrote on March 18, 2011 at 02:14
I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

Jims wrote on April 5, 2011 at 07:08
I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.

AndyFletcherfs wrote on April 12, 2011 at 01:56
Hey - I am definitely delighted to find this. cool job!

Codys wrote on April 14, 2011 at 05:23
I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

Michael Ps wrote on September 5, 2011 at 15:13
Someone asked what to do if you don`t want to use captcha but want to stop form from resubmitting on F5. Here is an article about this: How to stop form from submitting with page refresh using PHP

Lones wrote on September 8, 2011 at 14:48
I think, that you are not right. I suggest it to discuss. Write to me in PM.


Kountays wrote on February 25, 2013 at 08:37
Thanks. I had made a captcha but was searching how to rotate/distort text. And I found here. Thanks again :-)
:-}

Add Comment:

CAPTCHA