Captcha class [Simple 2 Use]
Auteur: ArieMedia - 09 april 2009 - 16:07 - Gekeurd door: Koen - Hits: 5802 - Aantal punten: 3.00 (1 stem)
Installatie
plaats mkimg.php ergens op je webserver en roep deze aan.
In dezelfde map moeten je fonts komen, en je map moet CHMOD 777 hebben voor de afbeelding.
Vergeet niet om in mkimg.php in de functie font, de fonts te declareren!
en wees er zeker van dat je ob_start(); en session_start(); gebruikt hebt!
Het gebruik kan zo:
<?php
ob_start();
session_start();
include('mkimg.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>"Captcha" by Arie2Zero</title>
</head>
<body>
<?php
$captcha = new secureForm;
if(isset($_POST['control'])) {
if($captcha->checkcode($_POST['ncode']) == true) {
echo '<h3>Code was goed</h3>';
} else { echo '<h3>Code was verkeerd</h3>'; $captcha->secureForm($captcha->code(6)); } // secureForm(code, width, height, maxlines)
} else { $captcha->secureForm($captcha->code(6)); }
echo '<img src="SecuredByArie.png" alt="Secured" /><br>
<p style="font-size: 10px;">De code bevat geen 0</p>';
echo '<p>Controle:<br>
<form method="post" action="index.php">
Toets code in om verder te gaan: <input type="text" name="ncode" /><br>
<input type="submit" name="control" value="Go On" />
</form></p>';
ob_end_flush();
?>
<?php include('mkimg.php'); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>"Captcha" by Arie2Zero</title> </head> <body> <?php $captcha = new secureForm; if(isset($_POST['control'])) { if($captcha->checkcode($_POST['ncode']) == true) { echo '<h3>Code was goed</h3>'; } else { echo '<h3>Code was verkeerd</h3>'; $captcha->secureForm($captcha->code(6)); } // secureForm(code, width, height, maxlines) } else { $captcha->secureForm($captcha->code(6)); } echo '<img src="SecuredByArie.png" alt="Secured" /><br> <p style="font-size: 10px;">De code bevat geen 0</p>'; <form method="post" action="index.php"> Toets code in om verder te gaan: <input type="text" name="ncode" /><br> <input type="submit" name="control" value="Go On" /> </form></p>'; ?>
|
Code: |
<?php
/*
THIS CLASS WILL CREATE A CAPTCHA, MAKE SURE YOU STARTED A SESSION.
AUTHOR: ARJAN VELDKAMP [ARIE2ZERO.NL]
VERSION: 0.7
RELEASED ON 29 APRIL 2009
Use secureform like this way to create a new image:
$captcha = new secureForm;
$captcha->secureForm($captcha->code(4), 'mycaptcha', 200, 40, 8);
it will make a new image and a session with a random code with the width of 200px, height of 40px and
between 4 and 8 lines with the name mycaptcha.png
make sure that the map wich contains this class has rights to write (CHMOD 777)
*/
class secureForm {
protected $code;
protected $ucode;
protected $width;
protected $height;
protected $image;
protected $lines;
protected $naam;
/**
* Constructor, will define all information this class needs
* input string, string, int, int, int
**/
public function secureForm($code='Code', $naam='captcha', $width=100, $height=30, $mlines=8) {
$this->code = str_split($code);
$this->ucode = $code;
$this->width = $width;
$this->height = $height;
$this->lines = $mlines;
$this->naam = $naam;
$this->image = $this->image();
}
/**
* This function will create the image based on imagesize and characters,
* it wil automaticly choose his position, pick a color and place dots and lines
**/
private function image() {
$imgH = $this->height - 20;
$img = imagecreatetruecolor($this->width, $this->height);
$wit = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $wit); // the background color
// add some dots.. because they are funny to..
$dots = 200;
for($i=0; $i<$dots; $i++) {
$red = rand(0, 200);
$green = rand(0, 200);
$blue = rand(0, 200);
$x = rand(0, $this->width);
$y = rand(0, $this->height);
$kleur = imagecolorallocate($img, $red, $green, $blue);
imagesetpixel($img, $x, $y, $kleur);
}
$pos = array();
// make the image variable
for($i=0; $i < count($this->code); $i++) {
// pick some colors
$red = rand(0, 200);
$green = rand(0, 200);
$blue = rand(0, 200);
if($i == 0) { $edge = rand(0, -20); } else { $edge = rand(-30, 50); }
$fColor = imagecolorallocate($img, $red, $green, $blue);
// define some areas to put the letters on
$pos[$i] = array('width' => $this->fontspace() *$i + 1, // position x
'height' => rand(20, $imgH), // position y
'edge' => $edge); // the edge
$imgWN = $this->fontspace() * $i;
imagettftext($img, 14, $pos[$i]['edge'], $pos[$i]['width'], $pos[$i]['height'], $fColor, $this->font(), $this->code[$i]);
}
// and now add some lines, just for the fun =D
$lines = rand(2, $this->lines);
for($i=0; $i<$lines; $i++) {
$red = rand(0, 200);
$green = rand(0, 200);
$blue = rand(0, 200);
$x1 = rand(4, $this->width -30);
$y1 = rand(4, $this->height - 20);
$x2 = rand($x1, $this->width);
$y2 = rand($y1, $this->height);
$kleur = imagecolorallocate($img, $red, $green, $blue);
imageline($img, $x1, $y1, $x2, $y2, $kleur);
}
imagepng ($img, $this->naam.'.png');
imagedestroy($img);
}
/**
* This wil calculate the spacing between the letters
**/
private function fontspace() {
$space = round($this->width / count($this->code), 0);
return $space;
}
/**
* Define all your fonts here, you have to put them in the same folder as mkimg.php
**/
private function font() {
$font = array(); // Define all your possible fonts here
$font[] = 'Market_Deco.ttf';
$font[] = 'Mouse_Deco.ttf';
$font[] = 'Fiesta.ttf';
$aFont = count($font) - 1;
$tFont = rand(0, $aFont);
return $font[$tFont];
}
/**
* This wil make a random code
* input int / output string
**/
public function code($maxint) {
$tekens = array_merge(range('A', 'Z'), range(1, 9));
$teken = array();
for($i=0; $i < $maxint; $i++) {
$wTeken = rand(0, count($tekens) -1);
$teken[] = $tekens[$wTeken];
}
$code = implode('', $teken);
$_SESSION['code'] = $code;
return $code;
}
/**
* Check of the posted code is even to the image
* input string / output true or false
**/
public function checkcode($gepost) {
if(strtoupper($gepost) == $_SESSION['code']) {
return true;
} else { return false; }
}
}
?>
<?php /* THIS CLASS WILL CREATE A CAPTCHA, MAKE SURE YOU STARTED A SESSION. AUTHOR: ARJAN VELDKAMP [ARIE2ZERO.NL] VERSION: 0.7 RELEASED ON 29 APRIL 2009 Use secureform like this way to create a new image: $captcha = new secureForm; $captcha->secureForm($captcha->code(4), 'mycaptcha', 200, 40, 8); it will make a new image and a session with a random code with the width of 200px, height of 40px and between 4 and 8 lines with the name mycaptcha.png make sure that the map wich contains this class has rights to write (CHMOD 777) */ class secureForm { protected $code; protected $ucode; protected $width; protected $height; protected $image; protected $lines; protected $naam; /** * Constructor, will define all information this class needs * input string, string, int, int, int **/ public function secureForm($code='Code', $naam='captcha', $width=100, $height=30, $mlines=8) { $this->code = str_split($code); $this->ucode = $code; $this->width = $width; $this->height = $height; $this->lines = $mlines; $this->naam = $naam; $this->image = $this->image(); } /** * This function will create the image based on imagesize and characters, * it wil automaticly choose his position, pick a color and place dots and lines **/ private function image() { $imgH = $this->height - 20; $img = imagecreatetruecolor($this->width, $this->height); $wit = imagecolorallocate($img, 255, 255, 255); imagefill($img, 0, 0, $wit); // the background color // add some dots.. because they are funny to.. $dots = 200; for($i=0; $i<$dots; $i++) { $x = rand(0, $this->width); $y = rand(0, $this->height); $kleur = imagecolorallocate($img, $red, $green, $blue); imagesetpixel($img, $x, $y, $kleur); } // make the image variable for($i=0; $i < count($this->code); $i++) { // pick some colors if($i == 0) { $edge = rand(0, -20); } else { $edge = rand(-30, 50); } $fColor = imagecolorallocate($img, $red, $green, $blue); // define some areas to put the letters on $pos[$i] = array('width' => $this->fontspace() *$i + 1, // position x 'height' => rand(20, $imgH), // position y 'edge' => $edge); // the edge $imgWN = $this->fontspace() * $i; imagettftext($img, 14, $pos[$i]['edge'], $pos[$i]['width'], $pos[$i]['height'], $fColor, $this->font(), $this->code[$i]); } // and now add some lines, just for the fun =D $lines = rand(2, $this->lines); for($i=0; $i<$lines; $i++) { $x1 = rand(4, $this->width -30); $y1 = rand(4, $this->height - 20); $x2 = rand($x1, $this->width); $y2 = rand($y1, $this->height); $kleur = imagecolorallocate($img, $red, $green, $blue); imageline($img, $x1, $y1, $x2, $y2, $kleur); } imagepng ($img, $this->naam.'.png'); imagedestroy($img); } /** * This wil calculate the spacing between the letters **/ private function fontspace() { $space = round($this->width / count($this->code), 0); return $space; } /** * Define all your fonts here, you have to put them in the same folder as mkimg.php **/ private function font() { $font = array(); // Define all your possible fonts here $font[] = 'Market_Deco.ttf'; $font[] = 'Mouse_Deco.ttf'; $font[] = 'Fiesta.ttf'; $aFont = count($font) - 1; $tFont = rand(0, $aFont); return $font[$tFont]; } /** * This wil make a random code * input int / output string **/ public function code($maxint) { for($i=0; $i < $maxint; $i++) { $teken[] = $tekens[$wTeken]; } $_SESSION['code'] = $code; return $code; } /** * Check of the posted code is even to the image * input string / output true or false **/ public function checkcode($gepost) { return true; } else { return false; } } } ?>
Download code (.txt)
|
|
|
Stemmen |
Niet ingelogd. |
|