Captcha generation script in PHPThis is a simple captcha script which we will use to develop using PHP and GD library functions. To keep the script simple we have not used many powerful tools available in GD library. After understanding this basic tutorial we can go for advance captcha generation script. There are five files used,
Captcha validation without reloading the page. By using JQuery we can validate the captcha entry of the user. We will send the data to our backend PHP script and display the output to the user. Here user stays in the same page and status of the validation is shown as message to the user.
captcha-image.phpInside PHP script first we will destroy any previous session and then crate a new session which we will use to store random string. This randomly generated string we will add to our image and ask the visitor to read and enter in the text box provided.
We store the randomly generated string in a session variable and we use the same string to generate the captcha image. The session variable is available in our captcha-demo-data.php page where we match both data in session variable and user entered string.
We change the header of the page as we are sending an image ( not an html code )
In the last part of the script we will create the image using GD library support.
captcha-demo1.phpWe have a simple web form inside which we will include the PHP file captcha-image.php to display the image.
We have added one button to reload the image by user if required. Now once user submit the form the details will be displayed at captcha-demo-data.php file. This file collects all the form data and using one if condition check if captcha string ( present in the session variable ) is same as user entered string or not.
Font size and aligning inside imageWe are declaring the height and width here.
$im = @ImageCreate (80, 20) // Width and hieght of the image.The text we are placing inside this image here
ImageString($im,5,5,2,$_SESSION['my_captcha'],$text_color);Inside this function we have Int 5 as font size, Int 5 as X position of text inside Image, Int 2 as Y position. By adjusting these values we can align text to center , increase size of font and better position them.
Based on the number of chars used, font size and image with the text string can be positioned.
Advance Script to generate complex ImagesWe can rotate each char inside the image by arbitrary angle. This will make difficult for the robots to read the text inside images. We can add noise by adding random lines to the image. This part is added in advance section. We have generated each character of the random string as an image and then rotate the same to an arbitrary angle.
This article is written by plus2net.com team.
GD functions in PHP for handling images & graphicsHow to check GD support in PHP
Header for an image in browser
Drawing of graphic lines using GD
Plotting line graph by drawing data from table
Adding vertical grid to line graph
Adding rectangles using imagerectangle()
Imagearc drawing arc and circle
Imagefilledarc drawing filled arc and circle
Resizing and adding border to Images
Script for Watermarking image on the fly using PHP GD
Generate image by using user input form data