Basic CAPTCHA

Very basic code demonstrating the use of the GD library by creating a simple CAPTCHA.
[code lang="PHP"] function randomString($length){
$pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
if(empty($length)){
$length = "10";
}

for($i=0; $i<$length; $i++){
$string .= $pattern{rand(0,61)};
}

return $string;
}

function captcha() {
header("Content-type: image/png");
$im = @imagecreate(100, 50) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 255, 0, 0);
$string = randomString();
imagestring($im, 10, 5, 5, $string, $text_color);
imagepng($im);
imagedestroy($im);
}

captcha();
?>[/code]

Related posts:

  1. Getting to know GD Create dynamic userbars with GD Introduction: Have you ever been on aim and received a message from one of your...
  2. Email Obfuscator [code lang="Javascript"] [/code]...
  3. Decimal to Roman Numeral Conversion [code lang="cpp"]#include #include using namespace std; string dec_to_numeral(int x) { int dec[13] = {1000, 900, 500, 400, 100, 90, 50,...
  4. imagettftext only displays yellow text Lately I have been doing some work with the PHP GD library. I wanted to put text on an image...
  5. Basic Logic The modern computer is perhaps one of the most complex and perplexing yet ever so simple creations known. The entire...

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.