Skriv inn tall og bokstaver slik du ser de over


Filen index.php <?php
$path 
".";
include_once(
"$path/Random.php");
include_once(
"$path/Image.php");
session_start();
$rand = new Random();

$_SESSION['verification_string'] = $rand->toString();
$_SESSION["md5_string"] = md5($rand->toString());

?>
<table width="40%">
  <tr>
   <td>
   <img src="Image.php?antispam=<?php echo rand(10000009999999); ?>" />
      <form action="validate.php" method="POST">
        <p> Skriv inn tall og bokstaver slik du ser de over </p>
          <input name="txtCode" type="text" size="30" />
        </p>
        <p>
          <input type="submit" name="Submit" value="Send melding" />
        </p>
      </form>
    </td>
  </tr>
</table>
<hr />Filen index.php
<?php
show_source
('index.php');
print 
"<hr /> Filen Random.php";
show_source('Random.php');
print 
"<hr /> Filen Image.php";
show_source('Image.php');
print 
"<hr />Filen validate.php";
show_source('validate.php');
?>
I tillegg har jeg en mappe som heter fonts, hvor flere .ttf-filer ligger.


Filen Random.php <?php
class Random{
    var 
$verification_string;

    function 
Random(){
        
$this->verification_string $this->createString();
    }
    function 
createString(){
    
// Initialisere variabel, så en unngår undefined variabler i noen system
        
$verification_string="";
        
// Setter lengden på capchafelt til 5
        
$capchaLength 5;
        for (
$i 0$i $capchaLength$i++) {
           
$verification_string .= $this->createRandomChar();
        }
        return 
$verification_string;
    }
    function 
toString(){
        return 
$this->verification_string;
    }

    function 
createRandomChar() {
    
// Genererer et tilfeldig nummer mellom 1 og 3
        
$randomValue mt_rand(1,3);
        switch (
$randomValue) {
        case 
1:
            
// små bokstaver
            
$randomValue mt_rand(97122);
            break;
        case 
2:
            
// tall
            
$randomValue mt_rand(4957);
            break;
        case 
3:
            
// Store bokstaver
            
$randomValue mt_rand(6590);
            break;
        }
        return 
chr($randomValue);
    }
}
?>

Filen Image.php <?php
session_start
();
if(isset(
$_GET['antispam'])){
    
$image = new Image($_SESSION['verification_string']);
    
$image->getImage();
}

class 
Image {
    var 
$image;
    var 
$code;
    function 
Image($c){
        
$code $this->split($c);
        
$this->createImage($code);
    }
    
    function 
getImage(){
        
header('Content-type:image/jpg');
        
imagejpeg($this->image);
    }
    
    function 
split($string){
        
$array = array();
        for(
$i =0$i<strlen($string); $i++){
            
$array[] = $string[$i];
        }
        return 
$array;    
    }
    
    
     
   function 
createImage($code) {
       
$height=60;
    
$width count($code)*30;
    
$this->image imagecreate($width,$height );
    
$this->setBackground();
    
$this->setBorder();
    
$this->setText($code);      
   } 
   
   function 
setBackground(){
           
$randcolR mt_rand(200,255);
        
$randcolG mt_rand(200,255);
        
$randcolB mt_rand(200,255);
           
$backcolor imageColorAllocate($this->image$randcolR$randcolG$randcolB);    
    }
    
    function 
makeNoice($color){
        
imagearc($this->imagemt_rand(1,200), mt_rand(1,50), mt_rand(50,100), mt_rand(1,200), mt_rand(12,25), mt_rand(1,50), $color);
    }
    
    function 
readDir($dir){
        
$dh opendir($dir);
        
$fontname = array();
        while((
$file readdir($dh)) !== false){
            
$fileinfo pathinfo($file);
            if(
strcmp($fileinfo['extension'], "ttf")==0){
                
$fontname[] = basename($file);
            }        
        }
        return 
$fontname;    
    }
    
    function 
setText($code){
        
$gdpath dirname($_SERVER["SCRIPT_FILENAME"]). "/fonts/";
        
putenv('GDFONTPATH=' $gdpath);
        
$fontname $this->readDir($gdpath);
        
$font $fontname[mt_rand(0count($fontname)-1)];
        
$randcolR mt_rand(0,50);
        
$randcolG mt_rand(0,50);
        
$randcolB mt_rand(0,50);
        
        for(
$i=0;$i<count($code);$i++) {
            
$col imageColorAllocate($this->image$randcolR$randcolG $randcolB );
            
$nm rand(050);              
              
//imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )
            
imagettftext($this->imagerand(16,24), mt_rand(-3030), (25*$i)+10mt_rand(25imagesy($this->image)-10 ), $col$font$code[$i]);
            
imagecolordeallocate($this->image$col);
            
$this->makeNoice($col);
        }
        
    }
   
   function 
setBorder(){
      
// Border
       
$nm rand(0100);
       
$col imagecolorallocate($this->image$nm$nm$nm);
       
imagerectangle($this->image0,0imagesx($this->image)-imagesy($this->image)-1$col);
       
imagecolordeallocate($this->image$col);
    }  
}
?>

Filen validate.php <?php
    session_start
();
    
$enteredText md5(strip_tags($_POST["txtCode"]));
    
$randomText $_SESSION["md5_string"];
    if(
equals($enteredText$randomText)){
            print 
"Dette ser du fordi du tastet inn riktig hemmelig kode!";
    }else{
        print 
"dette gikk ikke så bra";
    }
        
        
    function 
equals($str1$str2){
        
session_unset();
        
session_destroy();
        return 
strcmp($str1$str2)==;
    }        
    
    
?>
I tillegg har jeg en mappe som heter fonts, hvor flere .ttf-filer ligger.