This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ /** * Genrates a CAPTCHA code for a given length. * * @param $len Lengh for the CAPTCHA code. * @return string Generated code. */ function cham_captcha_code( $len = 5 ) { $pattern = "23456789BCDFGHJKLMNPRSTVWXYZ"; $code = ''; for ( $i = 0; $i < $len; $i++ ) { $code .= $pattern[mt_rand(0,27)]; } return $code; } $image = imagecreatefrompng(dirname(__FILE__) . '/captcha.png'); $color = imagecolorallocate($image, 0, 0, 0); $code = cham_captcha_code(); imagestring($image, 5, 10, 2, $code, $color); // Save the code on a session var @session_start(); $_SESSION['captcha_code'] = $code; header("Content-type: image/png"); imagepng($image); imagedestroy($image); ?>