Programming: CAPTCHA script for PHP

Upon surfing the net, I found some people searching and asking about how to make a CAPTCHA and where they can find a relevant Turing Code. I have a simple program which I have created a couple of months ago and I think this one can be of help.


<?php
/***************************************************************************
*
* Created : January 17, 2008
* Modified : March 29, 2008
* Copyright : (c) 2008 Carl Angelo Villegas
* Version : 0.7
* Written by : Carl Angelo Villegas
*
* You are encouraged to redistribute and / or modify this program under the terms of
* the GNU General Public License as published by the Free Software Foundation
* (www.fsf.org); any version as from version 2 of the License.
*
* It is required that this notice to be intact and removing will be illegal action
*
***************************************************************************/
session_start();
// 33 all valid characters
// Just add the characters here you want to include
$valid_chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7', '8', '9');
$code = "";
for($length = 0; $length != 7; $length++)
{
if($length != 2 && $length != 4 && $length != 6)
{
$code .= $valid_chars[rand(0, 24)];
}
else
{
$code .= $valid_chars[rand(25, 32)];
}
}
$text = $_SESSION['code'] = $code;
$image = imagecreate(120, 30);
$bgColor = imagecolorallocate($image, 0, 0, 0);
$textColor = imagecolorallocate($image, 250, 250, 250);
$lineColor = imagecolorallocate($image, rand(50, 100), rand(50, 100), rand(50, 100));
$font = ‘fonts/palatino.ttf’; //change the font to whatever you like
$font_size = 14;
$angle = rand(-3, 3);
$int_x = rand(1, 40);
$int_y = rand(17, 25);
for($i=1; $i!=30; $i++)
{
imageline($image, rand(1, 120), rand(1, 30), rand(1, 120), rand(1, 30), $lineColor);
}
imagefill($image, rand(0, 100), rand(0, 100), $bgColor);
imagettftext($image, $font_size, $angle, $int_x, $int_y, $textColor, $font, $text);
header(”Content-type: image/png”);
imagepng($image);
?>

Just copy and paste it and you can use the script.
This one will be on the login or to whatever it is.


<tr>
<td>
Security Code:
</td>
<td>
<input type="text" name="code" size="10" maxlength="7" autocomplete="off">
</td>
<td>
<a href="#captcha" onClick="javascript: document.images['captcha'].src = ‘image.php?’ + new Date().getTime();”><img name=”captcha” src=”image.php” style=”cursor: pointer”></a><br />
Click the picture to reload
</td>
</tr>
<tr>
<td colspan=”3″ align=”center” valign=”bottom” height=”50px”>
<input type=”submit” name=”submit” value=”Procceed to payment”>
</td>
</tr>

And the code to check if the visitor is a bot or not


if($_POST['code']!=$_SESSION['string'])
{
$errors[] = ‘Invalid Security Code’;
}

And now you’re done. Just make sure you have pasted the right code in the right position in the script.

Related posts:

  1. Programming: Temperature Converter
  2. Redirection from Blogger to Wordpress.com
  3. Ubuntu 8.04 - Hardy Heron Released

Related posts brought to you by Yet Another Related Posts Plugin.

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)


ss_blog_claim=943397671420990dde83214c28892d78