Captcha maken voor invulformulier

Status
Niet open voor verdere reacties.
Op een invulformulier op de site www.bfrc.be komt vaak één of andere flauwe plezante invullen met allemaal rare tekens enz. Denkelijk automatisch gemaakt met pc.
Nu raad men aan om een captcha te koppelen aan deze formulieren.
Wat en hoe te maken?

beste groeten en alvast dank voor de hulp
 
Op een invulformulier op de site www.bfrc.be komt vaak één of andere flauwe plezante invullen met allemaal rare tekens enz. Denkelijk automatisch gemaakt met pc.
Nu raad men aan om een captcha te koppelen aan deze formulieren.
Wat en hoe te maken?

beste groeten en alvast dank voor de hulp

als je in php werkt gebruik je deze code:
lees ook zeker deze link eens door
PHP:
<?php
session_start();
 

 class CaptchaSecurityImages {
 
   var $font = 'monofont.ttf';
 
   function generateCode($characters) {
      /* list all possible characters, similar looking characters and vowels have been removed */
      $possible = '23456789bcdfghjkmnpqrstvwxyz';
      $code = '';
      $i = 0;
      while ($i < $characters) { 
         $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
         $i++;
      }
      return $code;
   }
 
   function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
      $code = $this->generateCode($characters);
      /* font size will be 75% of the image height */
      $font_size = $height * 0.75;
      $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');
      /* set the colours */
      $background_color = imagecolorallocate($image, 255, 255, 255);
      $text_color = imagecolorallocate($image, 20, 40, 100);
      $noise_color = imagecolorallocate($image, 100, 120, 180);
      /* generate random dots in background */
      for( $i=0; $i<($width*$height)/3; $i++ ) {
         imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
      }
      /* generate random lines in background */
      for( $i=0; $i<($width*$height)/150; $i++ ) {
         imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
      }
      /* create textbox and add text */
      $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
      $x = ($width - $textbox[4])/2;
      $y = ($height - $textbox[5])/2;
      imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
      /* output captcha image to browser */
      header('Content-Type: image/jpeg');
      imagejpeg($image);
      imagedestroy($image);
      $_SESSION['security_code'] = $code;
   }
 
}
 
$width = isset($_GET['width']) && $_GET['width'] < 600 ? $_GET['width'] : '120';
$height = isset($_GET['height']) && $_GET['height'] < 200 ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 2 ? $_GET['characters'] : '6';
 
$captcha = new CaptchaSecurityImages($width,$height,$characters);
 
?>
 
Laatst bewerkt:
Dank voor de zeer snelle reactie.
Nog een vraag: waar moet ik die code plakken?
Ik had ze geplakt in de php bij het invulformulier, maar alles gaat nog zoals voorheen.

zoals hier:
<?
/*
mail.php - versie 1.3 (20051010)
Jimmy Cappaert <jimmy@priorweb.be>
http://www.priorweb.be

Wijziging versie 1.3
- veldcontrole is nu configureerbaar per veld ($BODY_FIELDS_CHECK)
- veldspecifiëring is aangepast ($BODY_FIELDS)
-Cedric Dubois <cedric.dubois@priorweb.be>

Wijzigingen versie 1.2
- mogelijkheid om veldcontrole uit te schakelen
- na het versturen de pagina forwarden naar een andere pagina ipv een melding te geven
-Jimmy Cappaert <jimmy.cappaert@priorweb.be>

Deze code heeft als doel te fungeren als een mailer voor een HTML-formulier.
U kan deze file specifiëren in de ACTION van uw HTML-formulier.
*/

ob_start();

// INVULSECTIE
// GELIEVE DIT EERST IN TE VULLEN ALVORENS HET FORMULIER IN GEBRUIK TE NEMEN

// Het adres naar waar de e-mail moet afgeleverd worden (verplicht)
// Dit kan in 2 vormen: Uw naam <uw@adres.be> of gewoon uw@adres.be.
$HEADER_TO = "Pastoor Frans <webmaster@federatie-zemst.be>";
// Het adres waar een CC van de e-mail moet afgeleverd worden (niet verplicht)
$HEADER_CC = "Federatiesecretariaat <cor44@skynet.be>";
// Het adres waar een BCC van de e-mail moet afgeleverd worden (niet verplicht)
$HEADER_BCC = "Cor <cor.verschueren@skynet.be>";
// De afzender van de e-mail
$HEADER_FROM = " Mailer Federatie Zemst <federatie@federatie-zemst.be>";
// Het onderwerp van de e-mail
$HEADER_SUBJECT = "Abonnement Federatieblad ";

// Tip: je kan hier altijd een file includen om eventuele tekstopmaak in te plaatsen.
// Includen kan op volgende manier: include("file:///C|/Users/Cor/AppData/Local/Temp/Rar$DI00.789/bestandsnaam.php");
// Je kan ook HTML code in de variablen zetten. (bv linebreaks, fonts, ...)

// Naam en beschrijving van de velden die verstuurd moeten worden
// Bv array('naam'=>'Naam', 'voornaam'=>'Voornaam');
$BODY_FIELDS = array( 'Naam'=>'Naam', 'Voornaam'=>'Voornaam', 'Straat'=>'Straat', 'Bus'=>'Bus', 'Postnr'=>'Postnr', 'Gemeente'=>'Gemeente', 'Emailadres'=>'Emailadres', 'Telefoon'=>'Telefoon', 'Vermelding_op_overschrijving'=>'Vermelding_op_overschrijving' );
// Tekst die bovenaan (boven de velden) van de e-mail moet staan. Gebruik \n voor een nieuwe lijn.
$BODY_HEADER = "Dit is het restultaat van het formulier dat verstuurd werd:";
// Tekst die onderaan (onderaan de velden) van de e-mail moet staan. Gebruik \n voor een nieuwe lijn.
$BODY_FOOTER = "Einde van het bericht.";
// Moet het IP-adres van de verzender van het formulier mee verzonden worden? (1 = ja / 0 = neen)
$BODY_SHOWIP = 1;

// Controle of de velden allemaal werden ingevuld? (1 = ja / 0 = neen)
$RESULT_DOCHECK = 0;
// Indien u enkel bepaalde velden wenst te controleren, geef deze hier op en vul
// hierboven 0 in ($RESULT_DOCHECK = 0;)
$BODY_FIELDS_CHECK = array( 'Naam'=>'Naam', 'Voornaam'=>'Voornaam', 'Straat'=>'Straat', 'Bus'=>'Bus', 'Postnr'=>'Postnr', 'Gemeente'=>'Gemeente', 'Emailadres'=>'Emailadres', 'Telefoon'=>'Telefoon', 'Vermelding_op_overschrijving'=>'Vermelding_op_overschrijving' );
// Welke boodschap moet er weergegeven worden wanneer een veld niet werd ingevuld?
// Na de boodschap volgt de betreffende veldnaam.
$RESULT_FIELDMISSING = "Volgend veld werd niet ingevuld:";

// Boodschap weergeven bij succesvol versturen van formulier, of redirect naar een andere pagina? (1 = boodschap / 0 = redirect)
$RESULT_SUCCESS_TYPE = 0;
// Boodschap na het succesvol versturen van het formulier. Gebruik \n voor een nieuwe lijn. (enkel als u bij $RESULT_SUCCESS_TYPE '1' hebt staan)
$RESULT_SUCCESS_MSG = "Het formulier werd verstuurd.";
// Pagina om naar te forwarden na het succesvol versturen van formulier. (enkel als u bij $RESULT_SUCCESS_TYPE '0' hebt staan)
$RESULT_SUCCESS_URL = "Dank.html";

// Boodschap na het niet succesvol versturen van formulier. Gebruik \n voor een nieuwe lijn.
$RESULT_FAIL_MSG = "Sorry, het formulier kon niet verstuurd worden. Probeer later opnieuw.";


// EINDE INVULSECTIE
// U BENT VRIJ DE CODE DIE HIERONDER STAAT AAN TE PASSEN, MAAR BEDENK DAT U DAN HIEROP
// GEEN SUPPORT MEER ZAL KRIJGEN



if (count($BODY_FIELDS) < 1) {
echo "Sorry, er moet minstens 1 veld zijn om te verzenden!\n";
return 0;
}

if($RESULT_DOCHECK == 1) {
foreach($BODY_FIELDS as $key=>$value) {
if(!$_POST[$key]) {
$STOP_SCRIPT = 1;
echo $RESULT_FIELDMISSING." ".$BODY_FIELDS[$key]."<br>\n";
}
}
} elseif(count($BODY_FIELDS_CHECK) > 0) {
foreach($BODY_FIELDS_CHECK as $value) {
if(!$_POST[$value]) {
$STOP_SCRIPT = 1;
echo $RESULT_FIELDMISSING." ".$BODY_FIELDS[$value]."<br>\n";
}
}
}

if($STOP_SCRIPT == 1) {
return 0;
}

$BODY_MESSAGE_FIELDS = "";
foreach($BODY_FIELDS as $key=>$value) {
$BODY_MESSAGE_FIELDS .= stripslashes($value.": ".$_POST[$key]."\n");
}

$BODY_HEADERS = "From: $HEADER_FROM\n";
$BODY_HEADERS .= "Cc: $HEADER_CC\n";
$BODY_HEADERS .= "Bcc: $HEADER_BCC";
$BODY_MESSAGE = $BODY_HEADER."\n\n";
$BODY_MESSAGE .= $BODY_MESSAGE_FIELDS."\n";
$BODY_MESSAGE .= $BODY_FOOTER."\n";

if($BODY_SHOWIP == 1) {
$IP = (getenv(HTTP_X_FORWARDED_FOR))
? getenv(HTTP_X_FORWARDED_FOR)
: getenv(REMOTE_ADDR);
$BODY_MESSAGE .= "IP-adres verzender: $IP";
}

mail($HEADER_TO, $HEADER_SUBJECT, wordwrap($BODY_MESSAGE), $BODY_HEADERS) or die($RESULT_FAIL);

if($RESULT_SUCCESS_TYPE == 1) {
echo $RESULT_SUCCESS_MSG;
} else {
header("Location: $RESULT_SUCCESS_URL");
}
?><?php
session_start();


class CaptchaSecurityImages {

var $font = 'monofont.ttf';

function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = '23456789bcdfghjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}

function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
$code = $this->generateCode($characters);
/* font size will be 75% of the image height */
$font_size = $height * 0.75;
$image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['security_code'] = $code;
}

}

$width = isset($_GET['width']) && $_GET['width'] < 600 ? $_GET['width'] : '120';
$height = isset($_GET['height']) && $_GET['height'] < 200 ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 2 ? $_GET['characters'] : '6';

$captcha = new CaptchaSecurityImages($width,$height,$characters);

?>
 
zo doe je het:

Usage

Copy and paste the above code and save it on your webserver as CaptchaSecurityImages.php. You will also need to place a copy of the "Monofont" font in the same directory as the CaptchaSecurityImages.php file. (Alternatively you can replace the line var $font = 'monofont.ttf'; with the name of whatever font you want to use)

You can download the captcha zip which contains all the files needed to implement the script including the required font.

Place the following code on your form. This will generate an image with a random string of characters along with the text field where the user will retype the code.

HTML:
<img src="CaptchaSecurityImages.php" />
Security Code: 
<input id="security_code" name="security_code" type="text" />

You can also specify certain options for the image by passing them as variables to CaptchaSecurityImages.php. The options available are the width and height of the image and the number of characters

HTML:
<img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" alt="captcha" />
<input id="security_code" name="security_code" type="text" />

Place the following in the code where the form is submitted to. This code will check what the user has typed matches the code in the image.

PHP:
<?php 
   session_start();
   if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
      // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
      unset($_SESSION['security_code']);
   } else {
      // Insert your code for showing an error message here
   }
?>
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan