starthtml
Registriert: 05.07.2006
eBN-Alter: 3J 8M 11T
Beiträge: 1369
Verwarnungen: 0
eBytes: 22746.11
|
Titel: Re: Re: Captcha - Bewertungen+Verbesserungen
Zitat von zwanzer:
Und wenn das Formular nie angezeigt wird?
(das war ja auch der Fehler bei mir)
Meinst du meinen Post? Wenn ja, dann hier meine Antwort 
Du musst ein Captcha Bild generieren, während das Formular generiert wird. Das machst du nicht über den img-Tag, sondern schon vorher, im PHP-Code. OOP ist da dann übrigens sehr sinnvoll, da du das ganze sehr gut wiederverwenden kannst. Dann übergibt dir die Captcha-Klasse den Link zur Bilddatei, die du dann anzeigst 
Wenn das Bild nicht angezeigt wird, ist trotzdem ein Captcha erstellt worden 
EDIT: ich hatte da mal etwas gebaut... :
PHP-Code:
<?php
/**
* Captcha Class
*
* This file contains the captcha class which is used to handle captcha requests
*
* @author Simon S. <info@starthtml.de>
* @version 1.0
*/
class StarthtmlContact_Captcha
{
public $letters = array('A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z',
'1','2','3','4','5','6','7','8','9');
public $dir;
public $validTime = 600; //10 minutes
/**
* Constructor
*
* @param string $form Link to form template file
*/
public function __construct()
{
$this->dir = './StarthtmlContact/pictures/captcha/';
//garbage collector
$this->collectGarbage();
}
public function collectGarbage()
{
$dir = opendir($this->dir);
while($file = readdir($dir))
{
if($file == '.' || $file == '..')
{
continue;
}
$explodeUnderScore = explode('_',$file);
$explodeExtension = explode('.',$explodeUnderScore[1]);
$time = (int) $explodeExtension[0];
if(($time + 600) < time())
{
unlink($this->dir . '/' . $file);
}
}
}
/**
* This function creates a new captcha, i.e. it calls several functions (create string, save image,...)
*/
public function createCaptcha()
{
$str = $this->createString(5);
$_SESSION['StarthtmlContactCaptcha'] = $str;
return $this->_saveImage($str);
}
/**
* This function checks whether a string is the same as the captcha saved in the session
*
* @param string $str String to be compared
* @return boolean true if it is the same, otherwise false
*/
public function checkCaptcha($str)
{
$session = $_SESSION['StarthtmlContactCaptcha'];
unset($_SESSION['StarthtmlContactCaptcha']);
if($str == $session)
{
return true;
}
return false;
}
/**
* This function checks whether a string is the same as the captcha saved in the session
*
* @param string $str String to be compared
* @return boolean true if it is the same, otherwise false
*/
public function createString($letters)
{
$numberElements = count($this->letters) - 1;
for($i = 0; $i <= $letters; $i++)
{
$str .= $this->letters[rand(0,$numberElements)];
}
return $str;
}
private function _saveImage($str)
{
$img = imagecreate(80, 25);
imagecolorallocate($img, 173, 216, 230);
imagearc($img, rand(20, 60), rand(0,25), 80, 20, rand(1,360), rand(1,360), imagecolorallocate($img, 150, 255, 150));
imagearc($img, rand(20, 60), rand(0,25), 80, 20, rand(1,360), rand(1,360), imagecolorallocate($img, 255, 150, 150));
imagearc($img, rand(20, 60), rand(0,25), 80, 20, rand(1,360), rand(1,360), imagecolorallocate($img, 150, 150, 255));
imagearc($img, rand(20, 60), rand(0,25), 80, 20, rand(1,360), rand(1,360), imagecolorallocate($img, 255, 255, 150));
imageline($img, rand(0, 80), rand(0, 25), rand(0, 80), rand(0, 25), imagecolorallocate($img, 255, 255, 255));
imageline($img, rand(0, 80), rand(0, 25), rand(0, 80), rand(0, 25), imagecolorallocate($img, 255, 255, 255));
imageline($img, rand(0, 80), rand(0, 25), rand(0, 80), rand(0, 25), imagecolorallocate($img, 153, 153, 153));
imageline($img, rand(0, 80), rand(0, 25), rand(0, 80), rand(0, 25), imagecolorallocate($img, 153, 153, 153));
$str = str_split($str);
$posX = 5;
foreach($str as $char)
{
imagechar($img, 10, $posX, rand(1, 10), $char, imagecolorallocate($img, 0, 0, 0));
$posX += 13;
}
$path = './StarthtmlContact/pictures/captcha/' . md5(microtime()) . '_' . time() . '.png';
imagepng($img,$path);
imagedestroy($img);
return $path;
}
}
Zuletzt bearbeitet von starthtml am 12.04.2009 um 12:34:52 Uhr
|