Code: Select all
<?php
session_start(); //Start session to store variables and stuff
$string_length=rand(4, 6); //How long the captcha is
//Create the captcha string
$rand_string='';
for($i=0;$i<$string_length;$i++)
{
//Insert random uppercase letter
$rand_string.=chr(rand(65,90));
}
//Image size
$width=100;
$height=36;
//Create the image
$img=imagecreatetruecolor($width, $height);
$black=imagecolorallocate($img, 0, 0, 0);
$gray=imagecolorallocate($img, 200, 200, 200);
imagefilledrectangle($img, 0, 0, $width, $height, $gray);
//Use this font
$font='BELLB.TTF';
$font_size=16;
//Find approcimately where centered text should be
$y_value=($height/2)+($font_size/2);
$x_value=($width-($string_length*$font_size))/2;
//Draw the string using a true-type function
imagettftext($img, $font_size, 0, $x_value,
$y_value, $black, $font, $rand_string);
$_SESSION['encoded_captcha']=md5($rand_string);
//Give the image to the browser
header("Content-Type: image/png");
imagepng($img);
?>Yes, this is a very weak CAPTCHA. I just want it to work, then I'll make it strong later.
I bet the answer's obvious, I'm just really bad at PHP at the moment. I'm much better with Java.