In some cases you may want your loging form to be able to prevent automatic login by a robot ( script ). To achieve this we can create a login form which displays an image showing random numbers. The login form will have an extra input field to enter the values shown. Before working on the login form we must take care of the script that create the verification image first. Here is the code : <?php // generate 5 digit random number // create the hash for the random number and put it in the session // create the image // use white as the background image // the text color is black // write the random number // send several headers to make sure the image is not cached // Date in the past // always modified // HTTP/1.1 // HTTP/1.0 // send the content type header so the image is displayed properly // send the image to the browser // destroy the image to free up the memory To create a five digit random number we use rand() function and specify that the random number must be between 10000 and 99999. We put the hash value of this random number in the session. This hash value will be used by the login script to check if the entered number is correct. Next we create a small image, 60 x 30 pixels, using imagecreate(). We set the background color to white ( RGB = 255, 255, 255 ) using imagecolorallocate() function. Note that the first call to imagecolorallocate() will always set the background color for the image. Then we set the text color as black ( RGB = 0, 0, 0 ). Feel free to change the color text to your liking. To print the random number to the image we use the function imagestring(). In the script above we call this function like this : imagestring ($image, 5, 5, 8, $rand, $textColor); The first argument passed to this function is the image handler ( $image ). The second one ( 5 ) is the font. You can choose from one to five where one is the smallest font. The third and fourth parameter is the horizontal and vertical coordinate where we will print the image. The top left corner is defined as 0, 0. The last one is the text color which is black as mentioned earlier. |
1
Votes
Votes
User Authentication with Image Verification using php
Posted By tamilsolai on May 08, 2010 FROM: advancedphptutorial.blogspot.com report abuse





Post new comment