Skip to main content

Utilizing PHP-GD To Make Captcha

As the first security door, made captcha to prevent brutal action from a robot. How do I create one? Find out more here.

Re-typing a line of random characters for reasons of security seems to be mandatory that you must do when making the registration process for membership online. Not only in the registration form online, similar things can be encountered during the process online transactions in which the transmitted data is data that is crucial.

Random character string that you need to reset the type is known by the term captcha. Captcha is made with the assumption that only men can see the whole picture and identify the characters shown for a reset. This is to prevent the brutal actions carried out auto-generated by script or robot system in disarray a website.

Programmer website utilizing GD library in PHP script that converts them to create a line of random characters into an image. This article will discuss how to make a captcha system by using GD library functions.

About PHP-GD

PHP is not only used to process text and numeric data that are then displayed in HTML format, but can also be used to generate and process images with a variety of formats (including JPEG, GIF, PNG) To do that, you can take advantage of image processing functions that are in the GD library.

GD library that PHP scripts run with the term is often called the PHP-GD. In addition to the basic images, such as point, line, and writing, you can also use the PHP-GD to manipulate the image or the image in accordance with the meaning you want to submit.

Enabling GD Library

Before processing an image with PHP-GD, you should make sure that the server you use supports the use of GD library functions. There are several ways you can do to see the GD library on the server you use. One is by using the function phpinfo ()

Phpinfo();
?>


Function phpinfo () will show a lot of information about the condition of PHP in your server. Included in the information on the status of PHP libraries (known as term extension) that can be used. You can find out the status of the GD library in the search for information about the PHP Information.

If there is information on the GD (GD Support: Enabled) then the server that you use already support the use of GD library first before you start the experiment with image-processing PHP script.

To activate the GD library, you simply open the file php.ini (PHP Configuration File) and make a few changes to remove the sign ";" (semicolon) at the line "extension = php_gd2.dll"

How the above is quite easy to do because the server that you use the server is localhost which is your computer. Then, what if you use a service provider's server hosting service website? Of course you do not have access rights to modify the configuration file is.

The majority of website hosting service providers have facilities to include PHP-GD on services, so you do not need to activate the GD library to use them. However, if the facility is not available, you can still activate the GD library to use the following simple script:

if(!extension_loaded('gd')){
if(strtoupper(substr(PHP_OS, 3)) == "WIN"){
dl('php_gd2.dll');
}Else{
dl('gd.so');
}
}
?>


Initially, the script above will make the checking of the extension 'gd' on the server. If not active, the function dl () will load the library on the server in accordance with the operating system used by the server. For servers with a base Windows OS, akan load file 'php_gd2.dll' while server with LINUX or UNIX base akan load file 'gd.so'

Make a Captcha with PHP-GD

After ensuring that the server that is used to support the use of PHP-GD, you can start the experiment with image processing in PHP
One application that is made by using PHP-GD captcha is. As discussed previously, a captcha is an image that shows the combination of random characters. The images generated by the captcha generator in which there is a function of PHP-GD
This time you will learn to create captcha and consists of three main files, namely image.php, action.php, and index.php

Image.php file will act as a captcha generator produce fruit 7 random characters. Meanwhile, file action.php function as a captcha validator akan test visitor response to the generated captcha. The file index.php is a form that will display the captcha and ask captcha response to it.

Captcha Generator

To create a captcha, first must determine what combination of characters that will be displayed. You can create a random character generator from the hash function md5 ()

//captcha generator

//make random character
$random character = md5(rand(0.999));
$secret_code = substr(random_character, 17, 7);

//save secret code as session
session_start ();
$ _SESSION ["secret_code"] = $secret_code;
?>


The process is further mengonversi characters into an image with PHP-GD. Typically, a captcha is created with a specific effect any posts with the intent to make the robot recognize karakteryang displayed. However, it would be a mistake if the characters that appear very difficult to recognize, even though human.

There are many ways to make the effect any posts on the captcha. In addition to using the algorithm to manipulate the image, any posts effects can also be generated from the election with a certain type of letter style that is quite unique. This time, the effect any posts captcha taken from the selection of a particular case can still be read if only by men.

You can use the function ImageTTFText () to put any posts in the image on PHP-GD. ImageTTFText function () will take a string which is the name of the file of this type of letter that will be used. File the letter should be placed in the same folder with a captcha generator.

Captcha script generator more have below:

//captcha generator

//make random character
$random_character = md5(rand (0.999));
$secret_code = substr($ random_character, 17, 7);

//save secret code as session
session_start();
$ _SESSION["Secret_code"] = $ secret_code;

//make image area
$width = 170;
$height = 70;
$image = ImageCreate($width, $height);

//allocate color that will be used
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$green = ImageColorAllocate($image, 0, 255, 0);

//allocate black color background
ImageFill($image, 0, 0, $ black);

//display the secret code on image
ImageTTFText($image, 30, 7, 10, 60, $green,"SoulMission.TTF", $secret_code);

//provide additional particles form a line
ImageRectangle($image, 0, 0, $width-1, $height-1, $green);
ImageLine($image, 0, $ height / 2, $ width, $ height / 2, $ green);
ImageLine($image, $width/2, 0, $width/2, $height, $green);

//member on a message browser about type of file
Header("Content-Type:image/jpeg");

//output as image
ImageJPEG($image);

//clean up memory
ImageDestroy ($image);
?>


In the script above, ImageCreate function (width, height) will create a blank image with the width and height as a place to put some components captcha image. In addition to these functions, there are other functions you can use to create an image, namely ImageCreateTrueColor (width, height) Although the parameters have the same, the two functions have a difference in the capacity of colors that can be used.
ImageCreate function (width, height) can be used to process images with up to 256 colors. This function is suitable to process images in the format. "Gif" Meanwhile, the function ImageCreateTrueColor (width, height) can be used to process images with color depth up to 32-bit.

After determining the image area, the program will be allocated a number of colors that will be charged at $ image. ImageColorAllocate function (resource image, int red, int green, int blue) will be allocated a color that will apply to the image resource with a combination of colors red, green, blue. Int value that can be provided in the red, green, and blue is in between 0-255 (color palette).

Once allocated a color, you can put color in the image resource through many ways, one of which is ImageFill (resource image, int x, inty, int color) function has four parameters, namely resource image, int x, int y, int and color. If the function will be used to color the entire member area image resource, then you simply write down the coordinates (0, 0) with the int color is already allocated in advance.

The next step is to put the text in the form of random combinations of characters as characters captcha. To put the text on the image, you can use the ImageString () function and ImageTTFText ().

ImageString function () has six parameters, namely resource image, int fontNumber, int x, int y, string text, int, and color. Meanwhile, the function ImageTTFText () has eight parameters, namely resource image, int size, int angle, int x, int y, int color, string fontFile, and text string. The main difference on the second function is the diversity of the letter / character that can be displayed. In the script above, we use the function ImageTTFText () to retrieve the resource type of the form of additional letters that have been provided a unique past.

After placing unique characters in the picture, you can add additional particles to sophisticated robots. Captcha project at this time, particles are used to form two lines are mutually perpendicular and have the picture in the middle. The colors have the same character shown.

To create a line, you can use the function ImageLine (resource image, int x1, int y1, int x2, int y2, int color) parameters x1 and y1 are the coordinates for the beginning of a line drawing, while the parameter x2 and y2 are the coordinates of the point indicates from the last line that you make.

The script also uses the above function ImageRectangle (resource image, int x1, int y1, int x2, int y2, int color) to make the borderline of the captcha image is. Similar to the function ImageLine (), parameter int x1, int y1 are the starting point to create a square followed by int x2, int y2, which is the end point the image is built.

Then the program will inform the browser that the file is an image file image.php with type jpeg. After that, the function ImageJPEG (resource image) will show the results of the end of the image that you created earlier. To note, at the time the program runs, the image processing done by the GD library in memory so that it needed a function that displays the overall results of processing these images. After the results are displayed, you can clean with a memory function ImageDestroy (resource image) as in the program.

Captcha validator

After creating captcha generator, you must create a captcha validator to validate the responses given to the visitors. Captcha validator will read the code stored in the secret session at the code generated by the captcha generator. Secret code will be compared with the responses of visitors who make a transaction.
Here is a script for captcha validator:



Form For Captcha

After creating captcha captcha generator and validator, you must create a form that will display a captcha that you make. In form, there are at least three elements, the captcha image, textbox, and button. To avoid rewriting the URL, you should use the POST method to send data from form to captcha validator



After that, you can perform testing in the captcha that you have created.

Comments

Popular posts from this blog

Redmi Officially Launches Redmi Watch

  Redmi officially released its initial smartwatch Redmi Watchin China. In terms of style, Redmi Watch looks similar to the Apple Watch. Redmi Watch has a square-shaped design with rounded corners. Redmi Watchcarries a 1.4 inch 2.5 D display with a resolution of 320 x 320 pixels. Its weight is quite light, just 35 grams.Redmi Watch brings water resistance technology with a depth of up to 50 meters. Redmi Watch brings a variety of sports activity discovery features, from running, cycling, swimming, and also much more. Redmi Watchhas numerous innovative features including a heart price detector and also a rest period tracker. All tracking tasks can be kept track of and also accessed through the Mi Fit application. Via this application additionally, users can establish the screen face on Redmi Watch. There are greater than 120 types of front views to select from for free. Redmi Watchcan get in touch with mobile using Bluetooth connection. This wise watch includes NFC assistance

Graphical User Interface in Java (Part 1 From 3)

Frankly, graphics-based applications are always more interesting than the application of command-line based. And the good news, building graphical applications in Java, it is not as difficult as imagined Java is a programming language first released by Sun Microsystems in 1995. Java philosophy of "Write Once, Run Everywhere" make Java become one of the popular programming languages today. According to the official website of Java, Java technology currently used in 4.5 billion devices (more than 800 million PCs, 1.5 billion gadgets, 2.2 billion smart cards) and still did not include use in printers, web-cam, games, car navigation systems, medical devices, etc Know Java If you've heard of Java, but not so deeply in the program, chances are you will be confused with terms such as Java 2 SDK, JDK, Java Applets, Java Script, Java Runtime Engine (JRE), Java SE 6, etc. All of the Java technology, but if you want to learn to develop Java applications on a PC, the Java SE (Standar

New Autoit: Debug Mode!

The virus is always ranks 10 most popular of the virus, even in the big 3 a few months back. How new variants in action? Follow this following discription Virus-like folder icon with the standard Windows default this is the older players in the world of viruses. Variannya very much, and comes with a variety of sizes. Of the first characteristics that never change is the use of a folder icon with the aim to deceive users. Antivirus others have to recognize that Autoit virus with a lmaut, Sohaned, or YahLover, known as our own Autoit.EE What Is Autoit ? Alleged that the author himself called Nhatquanglan the most likely came from Thailand. But we are not sure whether the new variants also come from the same manufacturer. For those of you who have never know what it is to Autoit. Autoit or full Autoit Script, is also the name of the applications that use automation to create a script, exactly automation, hotkeys, and scripting, as written in the site. In short, the script will be in autoi