FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Captch





kewiin
Hello.
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?
catscratches
I guess that it's because of the image being saved in the browser's cache in order to let your site load faster next time. Maybe you could solve it just by giving catch.php some random _GET arguments so that the URL will not be the same?
leontius
Yup, just modify the urls at each GET request.

I suggest an alternative to captcha at http://recaptcha.net which does all the captcha generation for you and lets you help the world (see the website for details; basically you help digitizing literatures).
mahirharoon
see my code :
Code:

<?php
   
if (extension_loaded('gd'));
 
   
      
   $string = $_SESS['validation_code'];
   
   @header ("Content-type: image/png");
   
   $string = preg_replace( "/(\w)/", "\\1 ", $string);
   
   $string = trim($string);
   imagestring($im, 5, 0, 2,  $string, $black);
   $img = @imagecreate(200, 50) or die("Cannot Initialize new GD image stream");
   $black = imagecolorallocate($img, 0, 0, 0);
   $white = imagecolorallocate($img, 255, 255, 255);
   $grey = imagecolorallocate($img, 100, 100, 100);   
   
   for($i=1; $i<=10; $i++){
   
      imageline($img, ($i*20), 0, ($i*20), 50, $grey);
   
   }
   
   for($i=1; $i<=5; $i++){
   
      imageline($img, 0, ($i*10), 200, ($i*10), $grey);
   
   }

   imageline($img, 1, 1, 199, 1, $grey);
   imageline($img, 1, 49, 199, 49, $grey);
   imageline($img, 1, 1, 1, 49, $grey);
   imageline($img, 199, 1, 199, 49, $grey);
   
   for ($i = 0; $i < 500; $i++){
   
      imagesetpixel($img, rand(1, 199), rand(1, 49), $grey);
      
   }
   
   imagepng($img);
   
   imagedestroy($img);   
      

}

else
return false;

?>
kewiin
ok. Thx but i found a great captch after googling some time Wink
Related topics
Wasze metody walki ze spameramiContact form or email address?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.