What is CAPTCHA???
-A CAPTCHA is a program that can tell whether its user is a human or a computer.
-They are colorful images with distorted text at the bottom of Web registration forms.
-A common kind of CAPTCHA that is used on websites requires that the visitor type the letters and numbers of a distorted image. This method is based on the fact that is difficult for computers to extract the text from the image while it is very easy for humans.
Why CAPTCHA is needed??
-They are very common these days because of spammers who create special scripts and programs that automatically submit forms.
-CAPTCHAs are used by many websites to prevent abuse from "bots," or automated programs usually written to generate spam. No computer program can read distorted text as well as humans can, so bots cannot navigate sites protected by CAPTCHAs.
-It prevents standard automated software from filling out a form, unless it is specially designed to circumvent specific CAPTCHA systems.
-Programmers have worked to create special algorithms that can read the distorted letters from images with the purpose of defeating Captcha images. So, strong captcha's must be used to ensure that spam bots will not pass and submit their information to forms.
How to create CAPTCHA codes in PHP with a CAPTCHA script?
-A captcha script is a small part of code that you put on your web forms in order to generate captcha codes.
-As you probably know, captcha codes are a popular way to detect and stop spam bots, as they cannot distinguish letters and numbers in an image if they are displayed in a noised way.
-Captcha scripts can be written in many languages, including PHP, ASP, and PERL. It is either easy or difficult to break captchas; it all depends on how the numbers and letters are displayed on the image. The more variation is added, the better ( ex. changing fonts randomly, overlapping letters and numbers, rotating each letter and number a bit to the left or to the right ) as this makes the captcha code more difficult to break.
Implementation of Captcha Using PHP:
Using Simple Php file we can easily create and validate Captcha. We can use this part of code only for php or apache server. It Involves Following Steps.
1. Create a simple file in any Php Builder Software and name it “php-captcha.php”
2. Write the following code and save.
1.
2. session_start();
3. $RandomStr = md5(microtime()); // md5 to generate the random string
4. $ResultStr = substr($RandomStr,0,5); //trim 5 digit
5. $NewImage =imagecreatefromjpeg("img.jpg"); //image create by existing image
//and as back ground
6. $LineColor = imagecolorallocate($NewImage,255,255,255); //line color
7. $TextColor = imagecolorallocate($NewImage, 255,0 ,0 ); //text color-white
8. imageline($NewImage,1,1,40,40,$LineColor);
9. imageline($NewImage,1,100,60,0,$LineColor);
10. imageline($NewImage,1,120,70,0,$LineColor);
11. imageline($NewImage, 1, 50, 60, 0,$Linecolor);
12. imagestring($NewImage, 10, 20, 10, $ResultStr, $TextColor);
13. $_SESSION['key'] = $ResultStr; // carry the data through session
14. header("Content-type: image/jpeg"); // out out the image
15. imagejpeg($NewImage); //Output image to browser
?>
Php-captcha.php
The above file is used to create a captcha image by using existing image “img.jpg”. The image should be in same folder. Else we have to give full path of the image.
In line 2, session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. It returns true if the session has already started successfully.
In line3, $RandomStr is a variable which is going to have random string value produced by built-in function md5().The argument macrotime() is a function which returns the current Unix timestamp in microseconds.
In line 4, the length of random string generated by md5() is reduced from 10 to 5.The resultant string is stored in $ResultStr.
In line 5 ,$NewImage is variable of type image which contains the image created from existing image “img.jpg” by php built in function imagecreatefromjpeg().
In lines 6 and 7,we are setting the color of the line and text which we are going to draw on to the existing image by using Php built-in function imagecolorallocate(). This function returns the color identifier which contains the RGB value .This function contains 4 arguments, namely the resource image and the values of R,G,and B.
In lines 8,9,10 and 11, we draw a line on to the existing image by using php built-in function imageline(). It contains 6 arguments, namely the resource image, the 4 points and the color of the line.
In line 12, the built-in function. imagestring() is used to embed the text to the existing image. It contains 6 arguments, namely resource image, font, position (x, y), color.
In line 13, the value of the random string will be saved for validation of the captcha string.
In line 14 and 15 ,the header and image browser is set by using function header() and imagejpeg() respectively.
3. Open new php file and write the following code :
form.php
• This code is a combination of PHP,HTML and JAVASCRIPT.
• At the beginning we have to call session_start() function to start the session.
• The JavaScript function focuson() is used to set the focus to textbox in the form.
• The HTML code is used to design a form and it calls JavaScript function when the form is submitted.
• The another JavaScript Function check() is used to check the validation.
• The second part of the PHP code is used to compare the text entered is valid or not by comparing value of the session variable with the current value of the text field.
Happy Coding...
2 comments:
hai
3li.h.s
Post a Comment