rvalkass
Feb 3 2007, 03:06 PM
OK, I recently had the need to create a PHP CAPTCHA system for a friend, and I am sharing this as a tutorial with the good people here at Trap17. I am sure you have all seen a CAPTCHA before (although you may not have known what it was called). They are the little codes you often have to enter when you register with a site, to make sure you are a person and not an automated script. Some common examples look something like this:  My system doesn't really do anything as fancy, but I think that it is slightly more readable that some of those that get generated. Everyone wants to see the final product before they start, so here are a few examples of what this script generates:  OK, lets get started. Open up your favourite text/code editor (Kate, Notepad, Dreamweaver...) and you're ready. First, we need to tell the browser what we are going to ouput at the end of this, seeing as it won't be standard HTML. CODE <?php Header ("(anti-spam-(anti-spam-(anti-spam-content-type:))) image/png"); Simple enough. We are creating an image. Its a PNG. Now we need some randomness in our process. I have used 2 random numbers here, one as a string to be encoded with MD5, and the other will be used to cut our 32 character MD5 output into a 8 character code. CODE $enc_num = rand(0, 9999); //This number will be encrypted $key_num = rand(0, 24); //This is used to choose which bit of our string to use in the image Our first number is anywhere between 0 and 9999. It doesn't really matter what the range is, its just used to give MD5 something to encrypt. Our second number has to be specifically in the range of 0 to 24. Why? MD5 creates a 32 character string, of which we only need 8 characters. To add some extra randomness, this number chooses our starting character. It can't go above 24 (32-8) or we get less than 8 characters. So, to generate our string: CODE $hash_string = substr(md5($enc_num), $key_num, 8); Now we can start on the actual image. Here is where you get some customisation. If you look at my example above, I have used 3 background images which I premade for my script to use. You can use as many or as few as you like, just add the file names to this array, and add that to your code so far. Make sure they are in the PNG format! My array looks like this (with the images in the same directory as the script): CODE $bgs = array("lipsum.png", "fibres.png", "rainbow.png"); Designing the backgrounds with an image editor allows you to make sure that the text will be readable on them. All of mine allow the text to be easily seen to a human, but not so easily to a computer. Now of course, we need to pick one of these images to use as the basis for our CAPTCHA. There is a useful function called array_rand which will select one item from an array at random. Bingo! CODE $background = array_rand($bgs, 1); The GD library (available with all good hosts, such as Trap17) allows us to create images to output, and we will use this now. First, we create an image from a premade PNG background with this oh-so-inventively named function: CODE $img_handle = imagecreatefrompng($bgs[$background]); Next, we set the text colour and size. I have used a white colour as it shows up well on all of my backgrounds. Try changing the values if white doesn't look good for you. CODE $text_colour = imagecolorallocate($img_handle, 255, 255, 255); $font_size = 5; I've also done some fancy central alignment on the text, despite the fact all my images have the same dimensions  It allows for expansion, and you can design images to fit your site design. CODE $size_array = getimagesize($bgs[$background]); $img_w = $size_array[0]; $img_h = $size_array[1]; That simply gets the width and height of the background in pixels. Now we do some maths to find where the top left of the text should go, compensating for the width of the text. CODE $horiz = round(($img_w/2)-((strlen($hash_string)*imagefontwidth(5))/2), 1); $vert = round(($img_h/2)-(imagefontheight($font_size)/2)); Lastly, we plug all this together. We add the text to the image, display it to the browser, then destroy the temporary file. CODE imagestring($img_handle, $font_size, $horiz, $vert, $hash_string, $text_colour); imagepng($img_handle); imagedestroy($img_handle); ?> If you have any problems then feel free to ask away and I'll do my best. In the future I may add the functionality for a PHP-generated background, random colours, custom fonts, and to apply some warping to the text. My aim is to still keep it readable though. I carried out a (very un-scientific) test using my scanner's OCR software and that was unable to read the text. I am sure that if someone was determined and bothered enough then they would be able to break it, but it adds that little extra layer of protection to prevent people submitting things over and over, or computers being used to creat hundreds of messages, accounts... I have also attached the completed source code and my images here too. Feel free to use them, and if you do it would be nice to see how you are using it. Also, feel free to offer improvements and suggestions. Demo: http://www.rvalkass.co.uk/CAPTCHA/
Reply
jlhaslip
Feb 3 2007, 03:41 PM
Very nicely done. Excellent Tutorial style and presentation. In spite of the testing which you have done, I would be concerned about the contrasting of the text against the backgrounds, so perhaps the text colour could be modified in this code snippet to blend the text into the chosen background a little bit more? There seems to be too much contrast of the text against the background and although your scanner didn't pick it up, other systems and methods may be able to deterine the text, so altering the colour might be required...? CODE $text_colour = imagecolorallocate($img_handle, 255, 255, 255); $font_size = 5;
Reply
rvalkass
Feb 3 2007, 04:41 PM
I've been thinking about that, and have tried a few things to combat it. Generating random colours was one option, but that can occasionally lead to one or two characters being a bit difficult to read. Another option was applying a filter across the image. I didn't know these existed until I started looking through the GD library's functions. Putting the following code just before the imagepng($img_handle); line creates a sort of jagged-blur effect across the image: CODE imagefilter($img_handle, IMG_FILTER_SMOOTH, 0.1); It does sacrifice human readability a bit, generating images like this:  Adjusting the number 0.1 creates some different levels of the effect. I've tried running edge detection on the original images, and it proved quite reliable at picking the characters out. Random colours made life a bit more difficult, but this was the best way to beat edge detection, as now it picks edges up all over the image. With pixel maps for each character I think it would still be quite easy for someone to decipher it, but it's still an added layer. I'll keep adjusting it and see what I can come up with.
Reply
QuickSilva
Feb 4 2007, 07:18 PM
Very nicely put and extremely simple to follow. Maybe you could make a little form showing how to implent this actually into a web page, e.g. a contact us form. Or you could assign a session to this, and use md5 encryption on it to process this that way. I have quickly made an example of this: Image.php:CODE <?php session_start(); //CAPTCHA GENERATOR //Created by Rob Valkass 2007 //Edited by QuickSilva //Feel free to distribute and use as you want //Just leave this notice and comments intact
//Email: webmaster@rvalkass.co.uk //Web: www.rvalkass.co.uk
//Set the header to say what sort of information we are giving the browser Header ("(anti-spam-content-type:) image/png");
//Generate 2 random numbers for use in our encryption $enc_num = rand(0, 9999); //This number will be encrypted $key_num = rand(0, 24); //This is used to choose which bit of our string to use in the image
//Use these to get a random string of numbers and letters using MD5 //We then cut the 32 char MD5 down to an 8 char string, starting from a random point $hash_string = substr(md5($enc_num), $key_num, 8);
//MD5 the $hash_string variable again $hash_md5 = md5($hash_string);
//Asign it to a session $_SESSION['captcha'] = $hash_md5;
//Create an array of the images available to us as backgrounds $bgs = array("lipsum.png", "fibres.png", "rainbow.png");
//Choose the background image using the handy array_rand function $background = array_rand($bgs, 1);
//Now to start creating the all important image! //Set the background as our randomly selected image $img_handle = imagecreatefrompng($bgs[$background]);
//Set our text colour to white $text_colour = imagecolorallocate($img_handle, 255, 255, 255);
//Set the font size $font_size = 5;
//Get the horizontal and vertical dimensions of the background image $size_array = getimagesize($bgs[$background]); $img_w = $size_array[0]; $img_h = $size_array[1];
//Work out the horizontal position $horiz = round(($img_w/2)-((strlen($hash_string)*imagefontwidth(5))/2), 1);
//Work out the vertical position $vert = round(($img_h/2)-(imagefontheight($font_size)/2));
//Put our text onto our image imagestring($img_handle, $font_size, $horiz, $vert, $hash_string, $text_colour);
//Output the image imagepng($img_handle);
//Destroy the image to free up resources imagedestroy($img_handle);
?> Form.phpCODE <form method="post" action="process.php"> <img src="image.php" border="0"><br /> <input type="text" name="image"><br /> <input type="submit" value="Submit"> </form> Process.phpCODE <?php session_start(); //Start the session $session = $_SESSION['captcha']; //Define the session we set in image.php $image = $_POST['image']; //Define the post $image = md5($image); //MD5 encrypt the post
if ($session == $image){ //if they have put the right text in echo "Correct!"; }else{ echo "Incorrect!"; } ?> I have not tested this, so there maybe a few errors.
Reply
Matt2
Feb 5 2007, 12:13 AM
I'm sure this will be great help to people who would like to implement a CAPTCHA image in their contact forum, thanks for the tutorial. Unlike others yours is very easy to read and understand. Once again thanks for creating it
Reply
Autumn
Feb 5 2007, 05:30 AM
Great tutorial! I can honestly say that I never took the time to think of the work involved in those things. Honestly, never cared to because they're annoying. However, seeing how they're made kinda makes me appreciate it. Again, great tutorial. Perhaps you'll have more coming in the future? We'll see, I guess.
Reply
speedybiz
Feb 14 2007, 11:09 AM
wow, i love this good security tutorial, never again spam activity come to my site next time...
Reply
Wargasmic
Mar 15 2007, 07:59 PM
nice work, this will save a lot of people forums getting spammed. Thanks,
Reply
Dooga
Mar 15 2007, 11:03 PM
How do you implement this into a current form, or as a filter to the form pages? I really like this tutorial, since it's simple and easier to setup than other scripts on the internet.
Reply
rvalkass
Mar 16 2007, 06:24 AM
QuickSilva very handily wrote an example above, showing how you can implement this into a form. Basically you have to pass the text used on the image as a variable along with what the user enters. When they submit the form they are on, the script the form is passed to will check if the variable with the image text, and the one with the user's text match. If they do then they passed the CAPTCHA, if not, then tell them to go back and do it again. In the script, the variable $hash_string contains the text that appears on the image. If you pass that along with the form data as usual then in the script you have set up which deals with the form, one of the first things it should do is check that the user entered field and the $hash_string variable match (it's up to you if you want to make it case sensitive). QuickSilva has used sessions, which is another way to pass the variable along. If you want some help implementing this then I am happy to help you if you PM me with the details.
Reply
Latest Entries
lailai
Apr 4 2008, 08:27 AM
Cool! I will put that in my site, thanks for sharing!
Reply
delivi
Mar 26 2008, 04:00 PM
if you want to use captcha in your website or blog and do not want to write your own code or need a more advanced captcha solution you can use reCaptcha
Reply
galexcd
Mar 26 2008, 07:19 AM
QUOTE(alex1985 @ Mar 25 2008, 08:46 PM)  So, how can I integrate it with my registration project If you would have bothered to read QuickSilva's post then you wouldn't be asking that. He laid out some excellent php code you can use. And perhaps if you can't do things like image CAPTCHA's even when they are so politely handed over to you on a silver platter, perhaps you shouldn't be doing projects as complex as you are doing.
Reply
alex1985
Mar 26 2008, 03:46 AM
So, how can I integrate it with my registration project
Reply
rvalkass
Mar 8 2008, 12:49 PM
This Captcha script just works with an image designed to confuse the edge detection software used by spammers. The system of typing an answer to a math problem is also a good idea, but requires a slightly different approach. However, if you are interested, I might write a tutorial for that, and a few other ideas for Captchas.
Reply
Recent Queries:--
image validation with php - 5.22 hr back. (2)
-
img_handle - 5.75 hr back. (1)
-
php captcha image validation - 8.38 hr back. (1)
-
simple captcha php - 9.22 hr back. (1)
-
captcha rob valkass - 13.46 hr back. (1)
-
php captcha javascript - 19.09 hr back. (1)
-
php image captcha tutorial - 19.97 hr back. (1)
-
php 3d captcha - 20.77 hr back. (1)
-
simple captcha script - 21.34 hr back. (1)
-
php simple captcha - 26.27 hr back. (1)
-
php image validation captcha image - 27.98 hr back. (4)
-
captcha image for form validation - 28.67 hr back. (1)
-
captcha image validation - 25.26 hr back. (2)
-
simpel captcha php - 38.58 hr back. (1)
Similar Topics
Keywords : simple, php, captcha, image, valification
- Background Image Swap Script
Change a Background Image based on clock time (15)
Image Rotator Script (another One)
easy to implement (0) In case you haven't noticed, I have a different Avatar display on the Forum each time the page
is refreshed. /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif"
/> For those of you who might want the script to do that, here is the one I am using: HTML
$filesp = glob('*.png'); if(empty($filesp)){ echo
'no images found...die br >'; die(); } else{ foreach ($filesp as
$file) { $img_array[] = trim($file); }....
Math Captcha Image Validation
(1) This tutorial will show you how to make a basic math CAPTCHA validtion form. This requires that you
have the GD library for PHP installed to work. This tutorial requires 2 files, login.php and
action.php. The first step is to create a sub-folder to store the temporary images, for the
purposes of this tutorial,this folder should be called images. Now upload a image in there called
verify.php and chmod just that image(not the folder) to 777 so that image can change as our
functions generate new images. Ok, after you've done that, we can get to the code: in login.p....
Image Gallery Tutorial Using Hoverbox
A php solution to coding the Hoverbox Image Gallery (14) As reported in another posting , there is an Image Gallery named Hoverbox from the Sonspring site
which is a pretty cool display method using CSS to have Thumbnail pictures double their size by
hovering over them. I liked the css included in the original Tutorial as found on the Sonspring
site , but I knew there was more than one use for the Hoverbox and took it upon myself to explore
the use of the Hoverbox on a site I webmaster. One thing that wasn't right was having to
hardcode the image tags, so the first version I wrote used php to fill the Hoverbox by rea....
How To Make Image To Highlight When It`s Mouseovered
(8) Place this code between and tags. CODE <script
language="JavaScript1.2"> <!-- function makevisible(cur,which){
strength=(which==0)? 1 : 0.2 if (cur.style.MozOpacity)
cur.style.MozOpacity=strength else if (cur.filters) cur.filters.alpha.opacity=strength*100 }
// --> </SCRIPT> Place the following code within all of the image tags you'd
like the effect to be applied. CODE
style="filter:alpha(opacity=20);-moz-opacity:0.2"
onMouseover="makevisible(....
Image Shack Mod
For Invision Power Board (2) Image Shack Mod For Invision Power Board ''These instructions will help you to install
'ImageShack on your Site' into the reply, quick-reply, and post-new-thread sections of your
Invision Power Board, thus giving your visitors the ability to upload images directly. After upload,
they may paste the images directly into their post box.'' CODE These instructions will
help you to install 'ImageShack on your Site' into the reply, quick-reply, and
post-new-thread sections of your Invision Power Board, thus giving your visitors the ability ....
Creating A Simple Image Viewer
Using Visual Basic 2005 Express Edition (3) I downloaded Microsoft's Visual Studio Express suite a few months ago, but only recently got
around to installing it. I have been practising with Visual Basic and making some rather basic
programs and utilities, but they contain most of the basic concepts. This tutorial will explain how
to create a basic image viewer, and I will try to explain each step from beginning to end as clear
as I can. To start you will need: Microsoft Visual Studio About 10-20 minutes free time OK,
first open up the Visual Basic part of the Studio. I am using the 2005 Express version, so....
How To Stop Image Hot Linking
for a selected directory. (17) Those of you that don't know what is meant by 'hotlinking', it is when someone directly
links to an image on your site so it will display on their site. This is what is called
'bandwidth theft' and being as accounts here have a limit on bandwidth, your bandwidth limit
could be exceeded by someone else hotlinking to your images. As most users of cPanel will know,
there is an option to disable hotlinking of images in the "Site Management Tools" section.
However, this disables hotlinking to all directories, what if you only want to disable hotlinki....
Image Rollovers In Javascript
A Write-Once, Use-Anywhere Approach (11) Tutorial: Image Rollovers w/ Javascript, by Rob J. Secord, B.Sc. (SystemWisdom) See a
working Sample of this Script! Download a ZIP containing all working files in this
tutorial! Note: If you are not interested in reading this entire tutorial and/or have a basic
understanding of the underlying concepts, you may safely skip to the Implementation section to get
the code! Description : A Dynamic Image Rollover Script tested to work in 4 major internet
browsers: MSIE, FireFox, Netscape and Opera. Using only Javascript combined with regular HT....
How To: Change An Image When A User Clicks On It
using both php and javascript (11) How To: Change An Image When A User Clicks On It using both php and javascript - a powerful
combination I have seen quite a few how tos offering a method of doing this but none of which
resembled my method of making use of both php and javascript. This code is fairly repetitive and
most of the functions are easy to pick-up if you haven't heard of them before. Here it is...
Create your two images. Call them anything you like, you'd just need to change their filenames
in $imgano $imgayes. In fact with this script you can easily create more tha....
Extending The Image Preloader With Php4
Dynamically adds your images to Preloader! (3) Tutorial: Extending the Image Preloader with PHP4, by Rob J. Secord, B.Sc. (SystemWisdom)
Second Tutorial in a Series of 2 Tutorials! Be sure to have read the First One here: "Image
Preloader With Progress Bar Status" See a working Sample of this Script! (Note: Preloads
100 images, some images are larger than others, and may take awhile for some people.)
Description : A Tutorial for Extending the Image Preloader with PHP4 to Dynamically Populate the
Array of Preloaded Images. This tutorial is the second in a series of 2 tutorials, and assume....
Image Preloader With Progress Bar Status
Pure Client-Side JavaScript tested in 4 Browsers! (22) Tutorial: Image Preloader with Progress Bar, by Rob J. Secord, B.Sc. (SystemWisdom)
Description : A Tutorial for a Client-Side Image Preloader with Dynamic Real-Time Progress Bar
Indicator written in JavaScript! Tested to work with 4 Major Internet Browsers: Firefox, MSIE,
Netscape, Opera (Complete sample solution provided at end of tutorial, just put it on your
web-server, add your images and go!) Intended Audience : Beginner to Intermediate Web
Developers. Although this tutorial will cover some advanced aspects of JavaScript, I will try to
explain....
Roll-over Image Links With Css
No preloading of images (2) Here is a way of having roll over image links without preloading images by using CSS and a table.
It's very useful for a list of links as in a side bar menu or header. It also solves not having
to make a seperate image for each different link by placing the desired text over the image. 1.
First you need an image which comprises of the main link image, the 'mouse over' image link
underneath plus you can have another for visited links. As for most roll over images, the images
should be the same size. The above example - button.gif is 100x60px with each lin....
A Little Introduction To 3d Studio Max
How to make a simple abstract image (9) This tutorial will teach you the basics of making abstract images in 3D studio max. In this example,
I used a simple sphere, and applied the “Noise” modifier. Then, I applied a transparent, blue,
plastic-like material to spice up the whole thing. Let’s start. First, make a sphere by selecting
the “Sphere” button in the “Standard Primitives” section, and draw somewhere in the center of the
perspective view. We will set the size of the sphere later on. The sphere I made looks like this,
yours can be different in size and color, but the only thing that is important is to....
Easy Image Rotate Tutorial
(0) Well, since I'm new, I don't know what has been posted and what hasn't, so here's an
easy-to-use php tutorial! CODE <?php @header("Pragma: nocache");
$URL1="Your Image Source"; $URL2="Your Image Source"; $URL3="Your
Image Source"; $URL4="Your Image Source"; $URL5="Your Image Source";
srand((double) microtime() * 1000); $random = rand(1,5);
if($random == 1) @header ("Location: $URL1")....
Simple Image Rotator
randomly rotate images (6) First, It's really confusing. Do you know any tutorials on Image Manipulation on PHP?
Here's another simple one: 1. Create a 5 image. 2. Rename them to something like:
image1.jpg; image2.jpg; and so on... 3. Create your PHP file (rotation.php) 4. Enter the
following code: CODE <?php header("content-type: image/jpeg");
readfile("image".mt_rand(1,3).".jpg"); ?> 5. Execute your
script.....
Looking for simple, php, captcha, image, valification
|
|
Searching Video's for simple, php, captcha, image, valification
|
advertisement
|
|