Requirements: PHP support with installed GD library.
Four files are needed:
- a PHP file that generates the captcha image
- a font file used in the captcha image
- a form page to enter the code shown on the captcha image
- a processing page that evaluates the generated and entered values
All four files should be placed in the same directory on your web server.
I The PHP file
1. Open Wordpad and insert this:
Code: Select all
<?php
$width = 100;
$height = 38;
$font = 'ITCBlkad.TTF';
$f_size = 35;
session_start();
$randomnr = mt_rand(1000, 9999);
$_SESSION['random_txt'] = md5($randomnr);
$im = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $width, $height, $black);
imagettftext($im, $f_size, 0, 22, 24, $grey , $font, $randomnr);
imagettftext($im, $f_size, 0, 15, 26, $white, $font, $randomnr);
header ('Content-type: image/gif');
imagegif($im);
imagedestroy($im);
?>
II The font file
1. Open your fonts directory (usually C:\WINDOWS\Fonts\ ), locate Blackadder ITC (ITCBlkad.TTF) and copy this file to another place on your local harddisk. Be sure that the file is spelled exactly as in the above php file (case sensitive).
III The form page
You can add the captcha protection onto an existing form. In that case you can skip steps 1-2 below.
1. Draw a Form Area (or use an existing Form) and change the Form Properties to:
- Action: next_page.php or maintain the existing action file (*.php extension!)
- Method: POST
- Encoding type: empty (remove all text), or maintain the existing value
2. Be sure there is a Push Button on the Form Area. Bring up the Button Properties dialog and change it to:
- Button type: Submit
3. Put an Editbox onto the Form Area. Bring up its Editbox Properties dialog and change it to:
- Name: captcha_code
4. Draw a HTML Object (can be part of the Form Area) of 100 x 38 (w x h). Bring up its HTML Properties dialog and insert at HTML:
Code: Select all
<img src="captcha.php" alt="Click for new image" title="Click for new image" style="cursor:pointer" onclick="this.src='captcha.php?'+Math.random()">
IV The processing page
You can add the captcha protection onto an existing processing page. In that case you can skip step 1 below.
1. Open a new page in WebBuilder or use an existing processing page.
- The name of this page equals the Form Action (step III-1), so in this example it is: next_page.
- The file extension should be: php (using the Page Properties dialog).
2. Bring up the Page HTML dialog and select Start of Page. Insert the next code (eventually before any existing code):
Code: Select all
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['captcha_code'],$_SESSION['random_txt']) && md5($_POST['captcha_code']) == $_SESSION['random_txt']) {
unset($_POST['captcha_code'],$_SESSION['random_txt']);
}
else {
echo '<b>The entered code was wrong.</b><br>';
echo '<a href="javascript:history.back()">Go Back</a>';
exit;
}
}
?>
1. You can choose another font for the captcha image. You can change the variables at the start of the script.
2. The file captcha.php can be replaced. There are many php scripts available for generating captcha images. Please make sure:
- that the file name is 'captcha.php'
- that the generated code is stored in a session variable as: $_SESSION['random_txt'] = md5($generated_code);
- that the dimensions (w x h) correspond with the HTML Object (step III-4)
3. This captcha solution was based on http://www.joriso.nl/verhaaltjes-webdev ... ptcha.html
Updated
25-11-07 Editbox name changed to 'captcha_code'
10-12-07 Added image and improved text
15-12-07 Improved html code (step III-4)
14-04-08 Simplified code (step IV-2)
07-09-08 Adjusted code at step IV-2. Now it works with the 'built-in' form processor too.