$background = imagecolorallocate($image, 0, 0, 0); // Make background transparent by setting the colour we are going to use as transparent imagecolortransparent($image, $background); // Fill it in with the background colour. This is required to make the background transparent imagefilledrectangle($image, 0, 0, 199, 49, $background); // function to generate captcha characters function captchaChars($hash) { // Generate a 32 character string by getting the MD5 hash of the servers name with the hash added to the end. // Adding the servers name means outside sources cannot just work out the characters from the hash $captchastr = md5($_SERVER['SERVER_NAME'] . $hash); return strtoupper($captchastr); // Make all our characters uppercase for clarity in the image } // Lets get the characters to show in the image or say 'error' if no hash submitted $str = !empty($_GET['hash']) ? captchaChars($_GET['hash']) : 'ERROR'; // Assign a colour for the text and lines. I've chosen a shade of grey $our_colour = imagecolorallocate($image, 140, 140, 140); // Lets add three random background lines for ($i = mt_rand(5, 8); $i <= 29; $i += 10) { imageline($image, mt_rand(0, 100), $i + mt_rand(-5, 5), mt_rand(0, 100), $i + mt_rand(-5, 5), $our_colour); } // Set a random horizontal starting position $x_pos = mt_rand(10, 20); // Lets loop through our string adding one character at a time in a randomish position // We start with the first character and then use every seventh character just for added randomness for ($i = 0; $i <= 28; $i += 7) { imagestring($image, 5, $x_pos, mt_rand(0, 12), $str[$i], $our_colour); // add a character from our string $x_pos += mt_rand(10, 18); // Move the horizontal position by a random amount
if (isset($_POST['submit'])) { $hash = !empty($_POST['hash']) ? preg_replace('/[\\W]/i', '', trim($_POST['hash'])) : ''; // Remove any non alphanumeric characters to prevent exploit attempts $captchacode = !empty($_POST['captchacode']) ? preg_replace('/[\\W]/i', '', trim($_POST['captchacode'])) : ''; // Remove any non alphanumeric characters to prevent exploit attempts // function to check the submitted captcha function captchaChars($hash) { // Generate a 32 character string by getting the MD5 hash of the servers name with the hash added to the end. // Adding the servers name means outside sources cannot just work out the characters from the hash $captchastr = strtolower(md5($_SERVER['SERVER_NAME'] . $hash)); $captchastr2 = ''; for ($i = 0; $i <= 28; $i += 7) { $captchastr2 .= $captchastr[$i]; } return $captchastr2; } if (!empty($captchacode)) { if (strtolower($captchacode) == captchaChars($hash)) { echo '<h3>The submitted characters were correct</h3>'; } else { echo '<h3>The submitted characters were WRONG!</h3>'; } } else { echo '<h3>You forgot to fill in the code!</h3>'; } } ?> </div> </body> </html>