Mega Code Archive

 
Categories / Php / Graphics
 

Number Verification, used to prevent form auto-completes

First code, the one that generates the code and checks it. Save as "numverify.php". --- <?php ################# # numverify.php # ################# if (!$num_ent) { $nums = array("5","6","7","8","d","e","1","2","3","4","f","g","h","i","j","k","9","0","a","b","c","l","m","n","o","p","q","x","y","z","A","B","C","D","r","s","t","u","v","w","E","F","G","N","O","P","Q","R","S","H","I","J","K","L","M","T","W","X","Y","Z","U","V"); // Creates 10 random array access keys to nums. $random_key = array_rand($nums, 10); $random = ""; for ($i=0;$i<10;$i++) { // Uses random access keys to access 10 random elements in the array. $random = $random . $nums[$random_key[$i]]; } echo "<img src=\"pic.php?random=$random\">"; echo "<form action=\"$PHP_SELF\" method=\"post\"> Please type the code you see above. (CASE-SENSITIVE) <input type=\"text\" name=\"num_ent\"><br> <input type=\"hidden\" name=\"random\" value=\"$random\"> <input type=\"submit\" value=\"Submit\">"; } else { if ($num_ent == $random) { // Af they get it right ... echo "Correct!"; } else {// And if they get it wrong ... echo "Incorrect. Go back and refresh the page for a new code."; } } ?> --- Second code, the one that generates image. Save as "pic.php". --- <?php ########### # pic.php # ########### header("Content-type: image/png"); $im = @imagecreate(95, 18) or die("Cannot Initialize new GD image stream"); // 95,18 is the size of the image; // height,width. Must be changed if // font is changed. $background_color = imagecolorallocate($im, 225, 225, 225); // background color R,G,B $text_color = imagecolorallocate($im, 233, 14, 91); // text color R,G,B imagestring($im, 5, 1, 0, $random, $text_color); // "5" is the font, can be changed // 0-1, but the size changes to the // dimensions must be changed. 1,0 // is the x,y location. imagepng($im); imagedestroy($im); ?>