gusucode.com > 同城苏州黄页系统php源码程序 > lib/verifycode.class.php
<? define( 'DICT', '23456789abcdefghjkmnpqrstuvwxyz23456789ABCDEFGHJKMNPQvwxyz23RSTUVWXYZ23456789' ) ; class verifycode { var $code ; var $requester = '' ; var $font_dir = '/../verifycode_fonts/' ; var $img_type = IMAGETYPE_PNG ; var $img_width = 160 ; var $img_height = 70 ; var $font_top = 30 ; var $font_left = 5 ; var $font_size = 20 ; var $ignore_case = true ; var $show_prompt = true ; /** * 创建随机种子 * * @return int */ function _make_seed() { list($usec, $sec) = explode(' ', microtime()) ; $seed = (float) $sec + ((float) $usec * 100000) ; mt_srand( $seed ) ; } function verifycode( $img_type = IMAGETYPE_PNG) { # 开启 session if( !isset($_SESSION) ) session_start() ; # 创建 if( !isset($_SESSION['_VerifyCodes']) ) session_register('_VerifyCodes'); $this->requester = empty($_SERVER['HTTP_REFERER']) ? '--- ---' : base64_encode($_SERVER['HTTP_REFERER']) ; $this->img_type = $img_type ; $this->font_dir = dirname(__FILE__).$this->font_dir ; } /** * 随即产生 验证码 * * @param int $code_len * @param string $dict * @return string */ function _create_code( $code_len = 6, $dict = '' ) { if( empty($dict) ) $dict = DICT ; verifycode::_make_seed(); $this->code = ''; $dict_len = strlen($dict) - 1; $code = '' ; for ($i = 0; $i < $code_len; $i++) $code .= substr($dict, mt_rand(0, $dict_len), 1); return $code ; } /** * 随即产生 验证码 * * @param int $code_len * @param string $dict * @return string */ function create_code( $code_len = 6, $dict = '' ) { $this->code = verifycode::_create_code( $code_len, $dict ); # 保存 验证码 $_SESSION['_VerifyCodes'][ $this->requester ] = $this->code ; return $this->code ; } /** * 输出图像 * * @return bool */ function out_img( $font_dir = '' ) { if( empty($this->code) ) return false; if( !empty($font_dir) ) $this->font_dir = $font_dir ; # 检查/定位 字体文件 $fontfile=array(); $d=opendir( $this->font_dir ); while($filename=readdir($d)) { if(!preg_match('/\.ttf$/i',$filename)) continue; $fontfile[]=$this->font_dir.$filename; } if( empty($fontfile) ) { print "no font file.\r\n"; return false ; } # 创建图像 -- $img = imageCreate($this->img_width, $this->img_height) ; # 定义颜色 -- $bc=200+rand(-30,30); ImageColorAllocate($img,$bc,$bc,$bc); $rush = array( 'white' => ImageColorAllocate($img, 0xff, 0xff, 0xff), // 白色比刷 'white1' => ImageColorAllocate($img, 240, 240, 250), // 白色比刷 'white2' => ImageColorAllocate($img, 240, 240, 230), // 白色比刷 'black' => ImageColorAllocate($img, 10, 10, 10), // 黑色比刷 'black1' => ImageColorAllocate($img, 20, 20, 10), // 黑色比刷 'black2' => ImageColorAllocate($img, 30, 30, 15), // 黑色比刷 'yellow' => ImageColorAllocate($img, 255, 255, 10), // 黄色比刷 'yellow1' => ImageColorAllocate($img, 210, 225, 10), // 黄色比刷 'yellow2' => ImageColorAllocate($img, 240, 210, 10), // 黄色比刷 'blue' => ImageColorAllocate($img, 10, 100, 200), // 蓝色比刷 'blue1' => ImageColorAllocate($img, 10, 160, 180), // 蓝色比刷 'blue2' => ImageColorAllocate($img, 10, 80, 250), // 蓝色比刷 'green' => ImageColorAllocate($img, 30, 180, 40), // 绿色比刷 'green1' => ImageColorAllocate($img, 20, 240, 60), // 绿色比刷 'green2' => ImageColorAllocate($img, 40, 200, 10), // 绿色比刷 ); $red = ImageColorAllocate($img, 255, 10, 10) ; // 红色比刷 # 绘制 -- $ix = $this->font_left; $code_len = strlen($this->code) ; for ($i = 0; $i < $code_len; $i++) { imagettftext($img, $this->font_size + rand(0,5)*2, rand(-10,10), $ix, $this->font_top+rand(0,5)*2, $rush[array_rand($rush)],$fontfile[array_rand($fontfile)], $this->code[$i]); $ix += 20+rand(0,10); } # 提示信息 if( $this->show_prompt ) { $this->ignore_case ? $alert_word = '无需区分大小写' : $alert_word = '请注意大小写' ; imagettftext( $img, 10, 0, 10, $this->img_height-5, $red, $this->font_dir.'simsun.ttc', $alert_word ); } # 输出 -- header("Content-type: " . image_type_to_mime_type( $this->img_type )); imagepng($img); // 输出 imagedestroy($img); // 释放内存 } /** * 检验用户输入的验证码是否正确 * * @return bool * */ function check_code( $code ) { if( $this->ignore_case ) { $session_code = strtolower( @$_SESSION['_VerifyCodes'][ $this->requester ] ) ; $user_code = strtolower( $code ) ; } else { $session_code = @$_SESSION['_VerifyCodes'][ $this->requester ] ; $user_code = $code ; } //pp(',',$this->requester,$session_code,$user_code); return $session_code == $user_code ; } /** * 释放验证码 * */ function destroy_code() { if( isset($_SESSION[ $this->requester ]) ) unset($_SESSION[ $this->requester ]) ; } } ?>