Yes you read it right…! 5 lines of code
I know its hard to believe but this has to be one of the simplest implementation of Captcha, mainly because i can’t find any easier ones.
So here goes, this is how it works:
At the top of the page with the form you want to Captcha protect add these 3 lines of code, this generates the Captcha and adds it all to a nice variable to print out later:
$captcha = new Captcha;
$captchaImage = $captcha->create();
Then in your form where you want the Captcha, usually at the bottom, just above the submit button add this line, enclosed in php tags of course, this adds the captcha elements to the form:
So that is the form part done, now we just need to verify what the user has put in. For this i am going to assume that you are verifying atleast one part of the users input, so we need to add this line into it:
This would probably fit into your current checking like so:
Of course we have to add these 2 lines at the top of this processing page aswell (these are not included in the ‘5′ because they have already been added.
$captcha = new Captcha;
And thats it, the class handles everything else, well you have to put the files on your web-server, create a new directory to store the images and make it writeable, but thats a one-shot 10 second job!
Now for a screenshot:

Now for demo and the code:
14 Responses for "Captcha – in 5 lines of php :)"
I should mention that you MUST have GD Enabled on your web-server PHP implementation.
This is usually implemented but if you cannot see the image and you cant see the problem, check that one
Jamie.
In Response to Octanes post on Digg.
When i said that it was 5 lines of code, this was meant as what the user would have to add to their code.
Nobody would be stupid enough to think that you could create a CAPTCHA in5 lines, that just dumb.
Sorry but this isn’t a captcha in 5 lines of code. It’s 5 lines to invoke it, but there’s an entire include file in the background. So
a) it’s not ‘a captcha in 5 lines’. To claim this is wrong.
b) this type of captcha, with an included class that’s invoked with just a few lines has been done many many times. I mean really – welcome to any function/class created in the last 10 years.
Your claim is like someone saying ‘an entire encyclopedia in two lines!’
include(‘encyclopedia.php’); // a million lines of code.
echo $encyclopedia_data;
This is a great tool. I’ve got it working perfectly but it runs into problems verifying the input. I suspect its because the form is posting to itself – if it finds a form field it validates the data using an external PHP file. If there are no problems it redirects to the script that adds it to the database. How would I implement this captcha using this system?
Thanks in advance
I can spot a few bad things in the code:
- colors are static
- string is positioned in a horizontal line
- word is stored in a cookie which is client-side
Nice try, but not safe.
You cannot use CAPTCHA also too reliably.
Read my article
I’m getting this error
Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error in /home/.fuse/sebstence/olympics.theletterate.com/captcha.class.php on line 123
Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in /home/.fuse/sebstence/olympics.theletterate.com/captcha.class.php on line 123
I have GD installed… am I missing something?
If you would like to have a look.
http://olympics.theletterate.com/upload.php
thanks for your help.
can you help out with this error which I got after adding the code:
Warning: opendir(/path/to/your/captchaImages/directory) [function.opendir]: failed to open dir: Invalid argument in C:\Program Files\xampp\htdocs\Clients\captchatest\captcha.class.php on line 161
Warning: imagepng() [function.imagepng]: Unable to open ‘/path/to/your/captchaImages/directory/CAPTCHA_1211001275_20_2456.png’ for writing in C:\Program Files\xampp\htdocs\Clients\captchatest\captcha.class.php on line 123
There was an error generating the Captcha – Check your directory path and permissions.
Who ever write this lib, learn one thing.
Never, never, never delete the content of a directory when you have no idea what can be in this directory!
In this example the imagedirectory, you wiped my whole htdocs with that shit.
It\’s so hard to check if there are other files in the temp dir?
Long lives backupping!
i have a problem, the captcha work ok on mozilla, but in Internet explorer 6.0 is never validates….
i mean, on IE6 $verify=0 ….
somebody know why??????
help!!!
Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error in /home/.fuse/sebstence/olympics.theletterate.com/captcha.class.php on line 123
Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in /home/.fuse/sebstence/olympics.theletterate.com/captcha.class.php on line 123
__________________________________________
Answer :
Fix it easily by changing your compression variable of imagepng, imagegif or imagejpg/imagejpeg into a 1-10 ranged number – instead of the PHP 4 1-100 standard.
find
$saveImage = imagepng($image, $imageFilename, 100);
replace
$saveImage = imagepng($image, $imageFilename, 5);
picture not showing. everything else works. my path is wrong? help pls
Hi,
Your class is great!. I tried to integrate this to my page. But i couldn’t.
Error :
Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\dbtest\captcha.php:209) in C:\xampp\htdocs\dbtest\captcha.php on line 160
Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\dbtest\captcha.php:209) in C:\xampp\htdocs\dbtest\captcha.php on line 79
Unable to set the cookie – Cookies must be enabled to use this form.
Please help.
Thanks.
livinghttp://www.rezepta.net – .Test your Spanish tapas skills this May with a Serrano ham unconstrained from the insensitivity of Andalucia. Relations guild a belittle mouldy after in the excite of it Spanish online delicatessen Orce Serrano Hams are giving away congratulatory 1kg wheels of Manchego cheese with every Serrano ham to up up a famed duo of Spanish flavour.
Each ham arrives with repository to advised carving and storage instructions, jealous muslin sock and artisan Manchego cheese from La Mancha. Why not open out some happen Spanish pock-mark with an accepted Serrano ham centerpiece this Wink at bank after-effect come what may and section in a position to ending your carving skills to the search!
Leave a reply