最全最新php验证码制作

2025-10-05 14:38:58

1、运行php开发环境,在运行的目录下新建test.php

小编用的是wamp开发环境,所以在www文件夹下新建一个test.php

2、用php编辑器打开test.php,将编码方式设置为unicode编码

开启session,定义文件输出头,

创建类的对象$img(后边定义的类名的verify),这一步是留到最后来写,

可以先跳过创建对象这一步,等类写好了再来创建

最全最新php验证码制作

3、定义类verify,定义类verify的属性,重写构造函数,对属性进行初始化

最全最新php验证码制作

4、数字验证码方法,用php自带rand( )函数随机生成数字并进行拼接

最全最新php验证码制作

5、字母验证码,首先使用php自带的chr( )函数得到大写和小写字母的数组,并用shuffle( )函数打乱排序,接着从数组中取出一定长度的字符来拼接做成验证码

最全最新php验证码制作

6、数字和字母验证码,将数字和大小写字母都放入新数组里去,然后用shuffle( )函数打乱,接着从数组中取出一定长度的字符来拼接做成验证码

最全最新php验证码制作

7、中文验证码,把中文验证码可能出现的中文放入到$font_str里,不建议使用太复杂的中文(容易引起错误),unicode下,中文编码,一个中文为3个字节,

分割中文字符串到数组里,接着从数组中取出一定长度的中文来拼接做成验证码

最全最新php验证码制作

8、生成验证码的函数需要使用到gd库,gd库的开启,(wamp环境下)wamp\bin\php\php5.5.12  文件夹下的php.ini文件,搜索gd,便能找到了,确保该项前面的#号不存在,修改后重启环境

最全最新php验证码制作

9、生成验证码的函数,主要是再用gd库的内置函数绘图(包括设置干扰的线条或者干扰点),函数内switch判断,可以根据类创建时传入的参数的不同,生成不同类型的验证码

最全最新php验证码制作

最全最新php验证码制作

10、创建类对象,并且传入参数,在步骤2里已经写了,是下边这两行代码

$img = new verify(120,40,5,16); $img->code_verify('font_verify');//参数digit_verify(默认纯数字)、alpha_verify(纯字母)、alnum_verify(数字和字母)、font_verify(中文)

最全最新php验证码制作

11、上一步默认使用中文验证码,故而显示的验证码是中文的

使用中文验证码时,ImageTTFText()需要设置字符的样式,需要用到.ttf文件,请务必保证路径的正确,相对路径/绝对路径都行

最全最新php验证码制作

12、本验证码的4种类型,都能使用,都经过测试的。

如果你觉得对你有帮助,麻烦点个赞哦,送个票哦,小编在此不胜感激!

13、最后,附上源码,供大家使用哦。

<?php/***验证码类,提供数字、字母、数组和字母、中文验证*/session_start();header("Content-type:text/html;charset=utf-8");header("Content-type: image/png"); $img = new verify(120,40,4,16,'point'); $img->code_verify();//参数digit_verify(默认纯数字)、alpha_verify(纯字母)、alnum_verify(数字和字母)、font_verify(中文)class verify{        private $width ;//图片宽度    private $height;//图片高度    private $code_length ;  //验证码长度    private $bkcolor;//背景颜色    private $fontcolor;//字体颜色    private $fontsize;//字体大小    private $noisetype;//干扰类型(点/线)    private $noisenum ;//干扰点的数量        //长、宽、干扰类型、验证长度    function __construct($width=120,$height=80,$code_length=4,$fontsize=24,$noisetype='line'){        $this->width = $width;        $this->height = $height;        $this->noisetype = $noisetype;        $this->fontsize = $fontsize;        $this->code_length = $code_length;    }        //数字验证码    function digit_verify(){        $length = $this->code_length;        $code = '';        for($i=0;$i<$length;$i++){            $num = rand(0,9);            $code .= $num;        }        return  $code;    }    //字母验证码    function alpha_verify(){        $length = $this->code_length;        $code = '';        $alpha_arr = array();        for($i=97;$i<=122;$i++){  //小写字母,对应十进制的97-122            $alpha_arr[] = chr($i);        }        for($i=65;$i<=90;$i++){  //大写字母,对应十进制的65-90            $alpha_arr[] = chr($i);        }        shuffle($alpha_arr);   //打乱数组,自动返回打乱后的数组        for($i = 0 ;$i <$length;$i++){            $alpha_site = rand(0,51);            $code .= $alpha_arr[$alpha_site];        }        return $code;    }        //数组与字母组合验证码    function alnum_verify(){        $length = $this->code_length;        $code = '';        $alnum_arr = array();        for($i=97;$i<=122;$i++){             $alnum_arr[] = chr($i);        }        for($i=65;$i<=90;$i++){             $alnum_arr[] = chr($i);        }        for($i=0;$i<10;$i++){             $alnum_arr[] = $i;         }        $unset_arr = array(0,'o','O','I','i','l');        foreach($unset_arr as $v){             $key = array_search($v,$alnum_arr);             unset($alnum_arr[$key]);        }        shuffle($alnum_arr);   //打乱数组,自动返回打乱后的数组        for($i=0;$i<4;$i++){        $alnum_site = rand(0,55);             $code .= $alnum_arr[$alnum_site];        }        return $code;    }    //中文验证码    function font_verify(){        $length = $this->code_length;        $code = '';        $font_str = '一二三四五六七八九十今天星期好饿困了猪牛羊马狗龙蛇鸡鼠兔烟笼寒水月笼沙夜泊情怀近酒家商女不知亡国恨隔江犹唱后庭花天向上好学习我晕云彩河水天气';        $font_arr = str_split($font_str,3);        $font_arr_length = count($font_arr);        for($i=0;$i<4;$i++){            $font_site = rand(0,$font_arr_length-1);            $code .= $font_arr[$font_site];        }        return $code;    }        function code_verify($verify_type='digit_verify'){        $width = $this->width;        $height = $this->height;        $noisetype = $this->noisetype;        //创建真彩色白纸        $im = @imagecreatetruecolor($width,$height) or die('创建图像失败');        //获取背景颜色        $background_color = imagecolorallocate($im,255,255,255);        // $background_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));        //填充背景颜色        imagefill($im,0,0,$background_color);        //获取边框颜色        $border_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));        //画矩形,填充边框颜色        imagerectangle($im,0,0,$width-1,$height-1,$border_color);        //逐行炫耀背景,全屏用1或0        if($noisetype == 'line'){            for($i=2;$i<5;$i++){                $noise_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));                imageline($im,rand(0,$width/2),rand(0,$height/2),rand($width/2,$width),rand($height/2,$height),$noise_color);            }        }else{            for($i=2;$i<500;$i++){                $noise_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));                imagesetpixel($im,rand(0,$width),rand(0,$height),$noise_color);            }        }        //内容颜色        $text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180));        $fontsize = $this->fontsize;        $code = '';        switch($verify_type){        case 'alpha_verify':                $code = $this->alpha_verify();                imagestring($im,$fontsize,rand(20,$width/2),rand($height/2,$height-20),$code,$text_color);                break;        case 'alnum_verify':                $code = $this->alnum_verify();                imagestring($im,$fontsize,rand(20,$width/2),rand($height/2,$height-20),$code,$text_color);                break;        case 'font_verify':                $code = $this->font_verify();                $fnt = "SIMLI.ttf"; //显示的字体样式 ,目录下需要有此文件                ImageTTFText($im,$fontsize,0,rand(0,$width/2-30),rand(20,$height/2+20),$text_color,$fnt,$code);                  break;        case 'digit_verify':                $code = $this->digit_verify();                imagestring($im,$fontsize,rand(20,$width/2),rand($height/2,$height-20),$code,$text_color);                break;        default:                $code = $this->digit_verify();                imagestring($im,$fontsize,rand(20,$width/2),rand($height/2,$height-20),$code,$text_color);        }        $_SESSION['mycode'] = $code;        imagepng($im);;        imagedestroy($im);    }}?>

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢