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:
- Programming: Temperature Converter
- Redirection from Blogger to Wordpress.com
- 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.


![[Ask]](http://www.randombatch.com/wp-content/plugins/bookmarkify/ask.png)
![[backflip]](http://www.randombatch.com/wp-content/plugins/bookmarkify/backflip.png)
![[Bloglines]](http://www.randombatch.com/wp-content/plugins/bookmarkify/bloglines.png)
![[Blogsvine]](http://www.randombatch.com/wp-content/plugins/bookmarkify/blogsvine.png)
![[del.icio.us]](http://www.randombatch.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.randombatch.com/wp-content/plugins/bookmarkify/digg.png)
![[Facebook]](http://www.randombatch.com/wp-content/plugins/bookmarkify/facebook.png)
![[Furl]](http://www.randombatch.com/wp-content/plugins/bookmarkify/furl.png)
![[Google]](http://www.randombatch.com/wp-content/plugins/bookmarkify/google.png)
![[linkaGoGo]](http://www.randombatch.com/wp-content/plugins/bookmarkify/linkagogo.png)
![[LinkedIn]](http://www.randombatch.com/wp-content/plugins/bookmarkify/linkedin.png)
![[Ma.gnolia]](http://www.randombatch.com/wp-content/plugins/bookmarkify/magnolia.png)
![[Mister Wong]](http://www.randombatch.com/wp-content/plugins/bookmarkify/misterwong.png)
![[Mixx]](http://www.randombatch.com/wp-content/plugins/bookmarkify/mixx.png)
![[MySpace]](http://www.randombatch.com/wp-content/plugins/bookmarkify/myspace.png)
![[Newsvine]](http://www.randombatch.com/wp-content/plugins/bookmarkify/newsvine.png)
![[Propeller]](http://www.randombatch.com/wp-content/plugins/bookmarkify/propeller.png)
![[Reddit]](http://www.randombatch.com/wp-content/plugins/bookmarkify/reddit.png)
![[Rojo]](http://www.randombatch.com/wp-content/plugins/bookmarkify/rojo.png)
![[Sphere]](http://www.randombatch.com/wp-content/plugins/bookmarkify/sphere.png)
![[Sphinn]](http://www.randombatch.com/wp-content/plugins/bookmarkify/sphinn.png)
![[Squidoo]](http://www.randombatch.com/wp-content/plugins/bookmarkify/squidoo.png)
![[StumbleUpon]](http://www.randombatch.com/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Technorati]](http://www.randombatch.com/wp-content/plugins/bookmarkify/technorati.png)
![[Twitter]](http://www.randombatch.com/wp-content/plugins/bookmarkify/twitter.png)
![[Windows Live]](http://www.randombatch.com/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.randombatch.com/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.randombatch.com/wp-content/plugins/bookmarkify/email.png)



Comments
No comments yet.
Leave a comment