Superior Web Solutions 905 532 9642
HOME CLIENTS SERVICES BLOG ABOUT US CONTACT US
Categories:
 Internet Marketing
 iPhone iPad Apps Development
 IT Humour
 IT Industry News
 Search Engines
 User Guides
 Web Development
Browse by tags
Search Blog:
RSS
Subscribe to our newsletter
Name*:
E-Mail*:
Recent Posts
Most Common Screen Resolutions in 2011
Make PHP CAPTCHA more secure
Updated Stats
What Googles New Search Security Measures Mean to You
GoDaddy Mod_Rewrite Problem
What is the default font size in Browsers?
October 05, 2009 - How to create CAPTCHA using PHP

Edit: Updated code is posted here: http://www.superiorwebsys.com/blog/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:
Lone 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.
Michael P 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
Cody 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.
AndyFletcherf wrote on April 12, 2011 at 01:56
Hey - I am definitely delighted to find this. cool job!
Jim 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.
Spepleenfox wrote on March 18, 2011 at 02:14
I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.
Prea wrote on February 26, 2011 at 12:01
hi, good site very much appreciatted
SaiweeliaCace 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.
Unio 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
Lait 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
Zaza wrote on November 7, 2010 at 01:20
Hello Everyone.I’m new to www.superiorwebsys.com, nice site!
dora wrote on November 2, 2010 at 11:07
Hello! Your site looks very professional, congrats!
Bob wrote on October 31, 2010 at 12:48
Great Blog. I add this Post to my bookmarks.
Jonathan wrote on August 15, 2010 at 22:56
Powerful post.
Kirby wrote on August 15, 2010 at 14:41
Wow,so niceeee!! Thanks for sharing!
Wine wrote on August 15, 2010 at 00:48
it was very interesting to read.
Lone wrote on August 14, 2010 at 21:21
you did a great job. The info is greatly appreciated
Gamer wrote on August 13, 2010 at 22:23
I fully agree with author opinion.
Tea wrote on August 12, 2010 at 23:21
this was my first time that i have visited you article and it is awesome ..
Chang 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.
Joe wrote on July 30, 2010 at 02:37
good share, great article, very usefull for us...thanks!
Johny wrote on July 29, 2010 at 09:25
thanks for this great post
Pete wrote on July 28, 2010 at 14:00
Thanks for posting!
Ben wrote on July 27, 2010 at 12:15
Great article Thank you so much!
MBT wrote on July 9, 2010 at 17:21
Thanks for posting! I really enjoyed the post. I`ve already bookmark this article.
James wrote on June 2, 2010 at 02:22
Thans for the nice post.
Ava KING 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.
Spanish John 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.
Free Samples wrote on April 9, 2010 at 10:41
This have to be the best site ever.
Johnzz 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
Snireeret wrote on March 17, 2010 at 19:39
Enjoying reading the posts here, thanks.
Lebront8 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!
Snireeret wrote on March 11, 2010 at 15:50
thanks for adding me to the forum.
ScooftUnceste 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
Gooner 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! :)
Tupseammece wrote on February 5, 2010 at 18:41
Hi my first comment whoopee...
Michael Pankratov 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

Michael Pankratov 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.

Ray 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?
Add Comment:
Name*:
E-Mail: Website:
Your message*:
»
© 2000 - 2012 Superior Web Solutions Web design    Web development    Online marketing