龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php 验证码从数字中选颜色不一样的

时间:2015-04-08 11:45来源:网络整理 作者:网络 点击:
分享到:
验证码从数字中选颜色不一样的 菜鸟自己写的**[PHP]代码**[代码片段(109行)]

菜鸟自己写的

[PHP]代码

<?php
session_start ();
header ( 'Content-Type:text/html;charset=utf-8' );

                            //画布宽,画布高,验证码0-?所有验证码的长度  有效验证码长度/验证码大小,颜色1红,2蓝,3黑
function checkcode($width,$height,$num,   $numlen ,$checknum,$fontsize,$colortype){

            //创建一个画布
            $img1 = imagecreatetruecolor ( $width, $height);
            //创建一系列颜色,
            $gray=imagecolorallocate($img1,200,200,200);
            $color1=imagecolorallocate($img1,240,240,240);
            $blue=imagecolorallocate($img1,0,0,255);
            $black=imagecolorallocate($img1,0,0,0);
            $red=imagecolorallocate($img1,255,0,0);
            $green=imagecolorallocate($img1,0,100,0);
            //创建一个随机产生的颜色
            $color = imagecolorallocate ( $img1, rand ( 0, 255 ), rand ( 0, 255 ), rand ( 0, 255 ) );

            imagefill ( $img1, 0, 0, $gray ); //填充北景色给验证码北景

            //生成一个要写入验证码的字串
            $str = "";          
            for($i = 0; $i <= $numlen; $i ++) {
                //随机生成下标,转为十六进制。
                $num1 = dechex ( rand ( 0, $num ) );
                $str .= $num1;
            }
            //创建一个原来要写入的副本。
            $str1 = $str; 

            //将原字符串随机生成四个下标位置的值替换成空格,然后,将原来的字符串生成一个副本,将变成空格的值记录下来,没变成空格
            //的值变成空格。
            //用数组来记录生成随机的要转成空格的位置,再判断,如果当前生成的空位置在前面出现过,从新生成,保证四次随机出现四个不一样的数。
            $seestr = ""; //将来记录下要存入的session值
            $arr = array ();
            //下面记录下以前随机出现过的数字,用来判断保证后面出的数是新的。
            $arr1 = array (); 
            for($i = 0; $i < $checknum; $i ++) {
                $b = rand ( 0, $numlen );
                while ( in_array ( $b, $arr1 ) ) { //如果当前位置在以前出现过,再来,直到是一个新下标位置为止。
                    $b = rand ( 0, $numlen );
                }
                $arr1 [] = $b;
                $arr [$b] = $str [$b]; //将要替换成空格的字符保存到一个数组。
                $str = substr_replace ( $str, " ", $b, 1 ); //循环得到的下标位置干掉,换成空格。
            }

            //对下标进行排序,从左至右保证输入的字符顺序与显示的验证码一致。
            ksort ( $arr ); //对下标进行排序。
            foreach ( $arr as $val ) {
                $seestr .= $val;
            }

        rand(1,2);

        //循环画线。
            $b=0;
            $c=rand(-8,6);;
            $d=$height; 
            $x=rand(2,6)    ;   
            for($a = rand(-8,6) ; $a <= 258 ; $a += $x) {   

                imageline ( $img1, $a, $b, $c,$d, $color1 );
                $c+=$x;             
            }

            //写入画布;         
            $width_left=$width-110;

            $x = rand ( 5, $width_left );
            $y = rand ( $fontsize, 0 ); 

            switch($colortype)
            {
                case 1 : $colortype = $red;
                break;
                case 2 : $colortype = $blue;
                break;
                case 3 : $colortype = $black;
                break;
                case 4 : $colortype = $green;
                break;
            }
            imagestring ( $img1, $fontsize,  $x, $y , $str1 , $colortype);
            imagestring ( $img1, $fontsize,  $x, $y , $str , $color );

            //session要记录住这个随机得到的字串,验证用户填写是否一致。          
            $_SESSION ['checkcode'] = $seestr;

            //将画布输出,要先规定输出的格式
            header ( "content-type:image/gif" );
            imagegif ( $img1 );
            //销毁画布
            imagedestroy ( $img1 );
}

//画布宽,画布高,验证码0-?所有验证码的长度  有效验证码长度 验证码大小,颜色方案
//保证验证码长度*字符大小要小于画布宽度。
checkcode( 170,25,16,10,4,5,rand(1,4));

?>
精彩图集

赞助商链接