PHP 高自定义性安全验证码的解决办法
内容摘要
这篇文章主要为大家详细介绍了PHP 高自定义性安全验证码的简单示例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:
<?p
对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:
<?p
文章正文
这篇文章主要为大家详细介绍了PHP 高自定义性安全验证码的简单示例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | <code class = "php" > <?php /** * 安全验证码 * * @param * @author php教程 www.idcnote.com **/ public function captcha(){ $font_dir = $_SERVER [ "DOCUMENT_ROOT" ] . "your_ttf_file.ttf" ; // 字体库 $img_w = 58; // 设置图片宽 $img_h = 20; // 设置图片高 $font_size = 11; // 字体大小 $angle_l = -10; // 左偏角 $angle_r = 10; // 右偏角 $code_str = "ABCDEFGHJKLMNPQRSTUVWXYZ36" ; $word_len = 4; // 验证码位数 $padding = 5; // 每两个文字之间间隔 $margin = 2; // 左侧边距 $base_line = 15; // 文字基线位置 $base_line_offset = 2; // 基准线偏移量 $pixel_num = 3; // 杂点数目基数 $pixel_color = 8; // 杂点只有 $pixel_color 种颜色 总的杂点数为$pixel_num*$pixel_color $noise_font_size = 1; // 杂点字体大小 $session_key = "my_captcha" ; //自定义session键名 header( "Cache-Control: no-cache, must-revalidate" ); header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); header( "Pragma: no-cache" ); header( "Cache-control: private" ); header( 'Content-Type: image/png' ); session_start(); $word = "" ; $code_str_len = strlen ( $code_str ) - 1; for ( $i = 0; $i < $word_len ; $i ++) { $word .= $code_str [rand(0, $code_str_len )]; } $_SESSION [ $session_key ] = strtolower ( $word ); $image = imagecreatetruecolor( $img_w , $img_h ); imagefilledrectangle( $image , 0, 0, $img_w - 1, $img_h - 1, imagecolorallocate( $image , mt_rand(235, 255), mt_rand(235, 255), mt_rand(235, 255))); //绘制杂点 for ( $i = 0; $i < $pixel_color ; $i ++){ $noise_color = imagecolorallocate( $image , mt_rand(150,225), mt_rand(150,225), mt_rand(150,225) ); for ( $j = 0; $j < $pixel_num ; $j ++) { imagestring( $image , $noise_font_size , mt_rand(-10, $img_w ), mt_rand(-10, $img_h ), $code_str [mt_rand(0, $code_str_len )], $noise_color ); } } //绘制文字 for ( $i = 0; $i < $word_len ; ++ $i ) { $color = imagecolorallocate( $image , mt_rand(0, 100), mt_rand(20, 120), mt_rand(50, 150)); imagettftext( $image , $font_size , mt_rand( $angle_l , $angle_r ), $margin , mt_rand( $base_line - $base_line_offset , $base_line + $base_line_offset ), $color , $font_dir , mb_substr( $word , $i , 1, 'utf-8' )); $margin += (imagefontwidth( $font_size ) + $padding ); } imagepng( $image ); imagedestroy( $image ); exit ; } /*** 来自php教程(www.idcnote.com) ***/ </code> |
注:关于PHP 高自定义性安全验证码的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释