Tutorial
CAPTCHA Spam Image
by jake | in PHP | posted December 02, 2007
![]()
![]()
![]()
![]()
(0 votes) | 859 views
In this short tutorial you will be able to make a CAPTCHA image to stop spammers.
Add to del.icio.us |
Digg this |
Dot This
In this short tutorial you will be able to make a CAPTCHA image to stop spammers.
To start off create your php headers
PHP:
<?php
?>
Then start a session for checking after the user has submitted the form
PHP:
<?php
session_start();
?>
Then we need to create a random string that contains numbers for recognition.
PHP:
<?php
$char = strtoupper(substr(str_shuffle('abcdefghjkmnpqrstuvwxyz'), 0, 4));
$str = rand(1, 7) . rand(1, 7) . $char;
$_SESSION['captcha_id'] = $str;
?>
This will create a string and add it to the session to check after the user has submitted a form. It will look like: 17QJEG or 74PUZA e.t.c.
Then we need to add the headers to tell the browser this is an image
PHP:
<?php
// Set the content type
header('Content-type: image/png');
header('Cache-control: no-cache');
?>
And then we need to add a background to make it harder for the bot to read the characters.
Here is an example: http://jake-hall.co.uk/captcha/button.png
This will create the image from that button.png inage.
PHP:
<?php
// Create an image from button.png
$image = imagecreatefrompng('button.png');
?>
This will set the font color. I have made it similar to the background image to make it hard for bots to read.
PHP:
<?php
// Set the span colour
$colour = imagecolorallocate($image, 183, 178, 152);
?>
We then need to specify what font we need to use. Use an unusual font just copy it from your C:/windows/font/ folder.
PHP:
<?php $span = 'span.ttf';?>
And now to make it even harder for the bot we are going to rotate the font slightly.
PHP:
<?php // Set a random integer for the rotation between -15 and 15 degrees
$rotate = rand(-15, 15);?>
Now put the text on the image and show it to the user.
PHP:
<?php // Create an image using our original image and adding the detail
imagettftext($image, 14, $rotate, 18, 30, $colour, $span, $str);
// Output the image as a png
imagepng($image);
?>
All done! Here is the final code for you to look at. Don't copy and paste as you don't learn that way.
Final code:
PHP:
<?php
session_start();
$char = strtoupper(substr(str_shuffle('abcdefghjkmnpqrstuvwxyz'), 0, 4));
$str = rand(1, 7) . rand(1, 7) . $char;
$_SESSION['captcha_id'] = $str;
//Copy and paster.
$str = "Copy and paster!";
// Set the content type
header('Content-type: image/png');
header('Cache-control: no-cache');
//Copy and paster.
$str = "Copy and paster!";
// Create an image from button.png
$image = imagecreatefrompng('button.png');
// Set the span colour
$colour = imagecolorallocate($image, 183, 178, 152);
// Set the span
$span = '/home/jake/www/span.ttf';
//Copy and paster.
$str = "Copy and paster!";
// Set a random integer for the rotation between -15 and 15 degrees
$rotate = rand(-15, 15);
//Copy and paster.
$str = "Copy and paster!";
// Create an image using our original image and adding the detail
imagettftext($image, 14, $rotate, 18, 30, $colour, $span, $str);
// Output the image as a png
imagepng($image);
?>

