Learn how to create a sign-up form for a website with a security image. The image prevents fake sign-ups and spam. In this tutorial, we will learn how to create a security image template, then put it to use.
The first page to create is the page which will generate the security images, securityimage.php. First, we need to open the PHP block and determine if we can use refid in the querystring; otherwise, we generate a unique one ourselves.
<?php //Generate Reference ID if (isset($HTTP_GET_VARS["refid"]) && $HTTP_GET_VARS["refid"]!="") { $referenceid = stripslashes($HTTP_GET_VARS["refid"]); } else { $referenceid = md5(mktime()*rand()); }
Now, we must select a font to use. For this demo, we will use Century, because it is easy to distinguish between zeroes and o's, and because it is a serif font, it is harder for a computer to comprehend. You will need to set the path to the font, reflecting what OS you are using and where the fonts folder is.
//Select Font $font = "C:\WINDOWS\Fonts\Century.ttf";
Next, we will randomly select one of the three backgrounds (from the included files). Using one of these files, we initialize a new image identifier $im, representing the background image, using ImageCreateFromPNG. If you want more variety, you can create more backgrounds (must be PNG format).
After this, we will generate a random string. To do this, we first create an array of characters to use. Then, we specify a length for the text to be generated. For this demonstration, we will use 8, but you can change it to any length you wish. Finally, we create an empty variable, then randomly fill it with characters in a loop.
//Generate the random string $chars = array("a","A","b","B","c","C","d","D","e","E","f","F","g","G", "h","H","i","I","j","J","k", "K","l","L","m","M","n","N","o","O","p","P","q","Q","r", "R","s","S","t","T","u","U","v", "V","w","W","x","X","y","Y","z","Z","1","2","3","4","5", "6","7","8","9"); $length = 8; $textstr = ""; for ($i=0; $i<$length; $i++) { $textstr .= $chars[rand(0, count($chars)-1)]; }