giovedì 3 gennaio 2013

ASCII Captcha

Why use an image to make a CAPTCHA? Use ASCII to do it (takes a few extra seconds to load but twice as secure)!



<html>
<head>
<title>Ascii</title>
<style>
#CAPTCHA
{
    font-size
:1px;
    line-height
:1px;
}
</style>
</head>
<body>
    
<?php
    
// Get the fonts for the CAPTCHA
    
$fonts glob'examples/CAPTCHA/fonts/*');
    
// Build a String
    
$string md5(rand(0,5000));
    
// Get the first 4 letters and place it in an array
    
$string substr($string,0,4);
    
$letters str_split($string);
    
// Set the CAPTCHA Height
    
$ih 60;
    
// Set the CAPTCHA Width
    
$iw 130;
    
// Set the Font size
    
$fs 25;
    
// Create an image
    
$image imagecreatetruecolor($iw,$ih);
    
// Set a default background color
    
$white imagecolorallocate($image255255255);
    
// Add the background color to the image
    
imagefilledrectangle($image00$iw$ih$white);
    
// Set minimum letter y position
    
$y_min = ($ih 2) + ($fs 3) - 10;
    
// Set maximum letter y position
    
$y_max = ($ih 2) + ($fs 3) + 10;
    
// Set the letter starting point
    
$x $fs;
    
// Set i to zero
    
$i 0;
    
// Loop through each letter
    
foreach($letters as $letter){
        
// Set the angle of the letter random from -45 and 45
        
$angle rand(-4545);
        
// Set a random y position of the letter using using the above min and max
        
$y rand($y_min$y_max);
        
// check to see if this is first letter, yes then skip next line otherwise,
        // add font size to x to move letter to the right
        
if($i != 0)
            
$x += $fs;
        
// Set the color of the letter
        
$font_color imagecolorallocate($imagerand(50,200), rand(50,200), rand(50,200));
        
// Choose a random font from our list of fonts
        
$rand array_rand($fonts);
        
// Add letter to the image
        
imagettftext($image$fs$angle$x$y$font_color$fonts[$rand], $letter);
        
// Incrament $i
        
$i++;
    }
    
// Finally build the image!
    
$black imagecolorallocate($image000);
    
// Build the ASCII image
    
echo '<div id="CAPTCHA">';
    for(
$h=0;$h<$ih;$h++){
        for(
$w=0;$w<=$iw;$w++){
            
$rgb imagecolorat($image$w$h);
            
$r = ($rgb >> 16) & 0xFF;
            
$g = ($rgb >> 8) & 0xFF;
            
$b $rgb 0xFF;
            if(
$w == $iw){
                echo 
'<br />';
            }else{
                echo 
'<span style="color:rgb('.$r.','.$g.','.$b.');">#</span>';
            }
        }
    }
    echo 
'</div>';
    
//End ASCII Image Build
    
$_SESSION['CAPTCHA'] = $string;
    
?>

<p>
    Image String:
<?php echo $_SESSION['CAPTCHA']; ?>
</p>
</body>
</html>

Nessun commento:

Posta un commento

Friends