| // +---------------------------------------------------------------------------+ /** * Provides methods for generating/validating a captcha. * * @package SGL * @author Steven Stremciuc */ class SGL_Captcha { /** * Returns random ascii art code for use as captcha. * * @access public * @static * @return string $captcha formatted ascii art captcha */ function generateCaptcha() { $aNumbers = array( array(' ### ',' # ',' ##### ',' ##### ',' # ',' ####### ',' ##### ',' ####### ',' ##### ',' ##### '), array(' # # ',' ## ',' # # ',' # # ',' # # ',' # ',' # # ',' # # ',' # # ',' # # '), array(' # # ',' # # ',' # ',' # ',' # # ',' # ',' # ',' # ',' # # ',' # # '), array(' # # ',' # ',' ##### ',' ##### ',' # # ',' ###### ',' ###### ',' # ',' ##### ',' ###### '), array(' # # ',' # ',' # ',' # ',' ####### ',' # ',' # # ',' # ',' # # ',' # '), array(' # # ',' # ',' # ',' # # ',' # ',' # # ',' # # ',' # ',' # # ',' # # '), array(' ### ',' ##### ',' ####### ',' ##### ',' # ',' ##### ',' ##### ',' # ',' ##### ',' ##### '), ); // randomly pick 4 numbers $aCode = array_rand($aNumbers[0], 4); // get code $code = ''; foreach ($aCode as $digit) { $code .= $digit; } // set code in users session SGL_Session::set('captcha',$code); // turn code into ascii art numbers $captcha = ''; for ($i = 0;$i < 7;$i++) { foreach ($aCode as $digit) { $captcha .= $aNumbers[$i][$digit]; } $captcha .="\n"; } return $captcha; } /** * Validates input against captcha code in users session. * * @access public * @param string $captcha * @static * @return bool */ function validateCaptcha($captcha = '') { if (!empty($captcha) && SGL_Session::get('captcha') == $captcha) { return true; } return false; } } ?>