Hello.
Im doing a captch function to my comment page.
captch.php
and i show the image like any other image.
<img src="captch.php" border="0">
But if the captch code is wrong then i got to go back and the session updates but not the captch.php image.
So how do you do you captch?
Im doing a captch function to my comment page.
captch.php
| Code: |
| <?php
session_start(); // Dir where the images are located $dir = "captch"; // A few setings... $font = "$dir/trebuc.ttf"; // Font-type... $size = 30; // Font-size... $spacing = 15; // Initial spacing of first char $height = 38; // Height from the top of the image $blur = 2; // X times the value of 10; // The CAPTCHA code $captcha = $_SESSION['sess_captcha']; // Create a 200 x 50px picture $im = imagecreatetruecolor(200, 50); // Get the background $background = ImageCreateFrompng("$dir/back.png"); // First layer $layer1 = ImageCreateFrompng("$dir/layer1.png"); // Second layer $layer2 = ImageCreateFrompng("$dir/layer2.png"); // Get a random number for the x and y cordinates $rand_x = mt_rand(0, 400); $rand_y = mt_rand(0, 550); // Add the background imagecopy($im, $background, 0, 0, $rand_x, $rand_y, 200, 50); for($i=0; $i < strlen($captcha); $i++) { // Randomize a color for each char! $color = imagecolorallocate($im, mt_rand(230, 255), mt_rand(230, 255),mt_rand(230, 255)); // Add the chars imagettftext($im, $size, mt_rand(-20, 20), $spacing, $height, $color, $font, $captcha{$i}); $spacing += 50; // Adds width between the chars } // Apply Gaussian Blur to the text, only applies if PHP5 is being used!! if (function_exists('imagefilter')) { for ($i = 0; $i < $blur; $i++) imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR, 10); } // finally, add 2 more layers imagecopy($im, $layer1, 0, 0, $rand_x, $rand_y, 200, 50); imagecopy($im, $layer2, 0, 0, $rand_x, $rand_y, 200, 50); // Set a few headers... header("Pragma: no-cache"); header('Cache-Control: no-store, no-cache, must-revalidate'); header("Content-type: image/jpeg"); // Display the generated picture imagejpeg($im, '', 80); imagedestroy($im); ?> |
and i show the image like any other image.
<img src="captch.php" border="0">
But if the captch code is wrong then i got to go back and the session updates but not the captch.php image.
So how do you do you captch?
