当前位置:首页>软件介绍>宝典php验证码制作 查询:
     
宝典php验证码制作

        我是PHP初学者,最近看了php100教程视频,参考了网上的一些资料,做了一个简简单单的php类封装验证码,跟大家分享一下。

        verification_code.php

        <?php

        /****文档注释*********************************************************

        描述:创建验证码图片

        *********************************************************************/

        session_start();

        /*********************************************************************

        实例化验证码类VerificationCodeImage

        输入:,,$mode=7

        $v_num=4

        $img_w=65

        $img_h=20

        $int_pixel_num=80

        $int_line_num=5

        $font_dir="fonts/"

        输出:,,验证码图像

        *********************************************************************/

        $vi,=,new,VerificationCodeImage(7,4,65,20,80,5,"fonts/");

        class,VerificationCodeImage

        {

        private,$mode;, //1:数字模式,2:字母模式,3:数字字母模式,4:汉字模式,5:数字汉字模式,6:字母汉字模式,7:数字字母汉字模式

        private,$v_num;, //验证码个数

        private,$img_w;, //验证码图像宽度

        private,$img_h;, //验证码图像高度

        private,$int_pixel_num;,//干扰像素个数

        private,$int_line_num;, //干扰线条数

        private,$font_dir;,,, //字体文件相对路径

        /*********************************************************************

        函数名:__construct

        描述:,,初始化类VerificationCodeImage

        输入:,,$mode=验证码模式

        $v_num=验证码个数

        $img_w=验证码宽度

        $img_h=验证码高度

        $int_pixel_num=干扰像素个数

        $int_line_num=干扰线条数

        $font_dir=验证码字体所在目录

        输出:,,验证码图像

        *********************************************************************/

        function,__construct,($mode,$v_num,$img_w,$img_h,$int_pixel_num,$int_line_num,$font_

        dir)

        {

        $this->mode,=,$mode;

        $this->v_num,=,$v_num;

        $this->img_w,=,$img_w;

        $this->img_h,=,$img_h;

        $this->int_pixel_num,=,$int_pixel_num;

        $this->int_line_num,=,$int_line_num;

        $this->font_dir,=,$font_dir;

        $this->CreateImage();

        }

        /*********************************************************************

        函数名:GetCodeChar

        描述:,,获得验证码内容

        输入:,,$mode=验证码模式

        输出:,,验证码内容

        返回值:$ychar=验证码内容

        *********************************************************************/

        function,GetCodeChar,($mode)

        {

        if,($mode,==,"1")

        {

        $ychar,=,"0,1,2,3,4,5,6,7,8,9";

        }

        elseif,($mode,==,"2")

        {

        $ychar,=,"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";

        $ychar.="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";

        }

        elseif,($mode,==,"3")

        {

        $ychar,=,"0,1,2,3,4,5,6,7,8,9,";

        $ychar.=,"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";

        $ychar.="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";

        }

        elseif,($mode,==,"4")

        {

        $ychar,=,"一,二,三,四,五,六,七,八,九,十,个,百,千,万,零,亿,小,中,大,多,少,高,

        低";

        }

        elseif,($mode,==,"5")

        {

        $ychar,=,"0,1,2,3,4,5,6,7,8,9,";

        $ychar,.=,"一,二,三,四,五,六,七,八,九,十,个,百,千,万,零,亿,小,中,大,多,少,

        高,低";

        }

        elseif,($mode,==,"6")

        {

        $ychar,=,"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";

        $ychar.="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";

        $ychar,.=,"一,二,三,四,五,六,七,八,九,十,个,百,千,万,零,亿,小,中,大,多,少,

        高,低";

        }

        elseif,($mode,==,"7")

        {

        $ychar,=,"0,1,2,3,4,5,6,7,8,9,";

        $ychar.=,"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";

        $ychar.="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";

        $ychar,.=,"一,二,三,四,五,六,七,八,九,十,个,百,千,万,零,亿,小,中,大,多,少,

        高,低";

        }

        return,$ychar;

        }

        /*********************************************************************

        函数名:RandColor

        描述:,,获得三原色

        输入:,,$rs=红

        $re=红

        $gs=绿

        $ge=绿

        $bs=蓝

        $be=蓝

        $rs,$re,$gs,$ge,$bs,$be均为数字,取值范围为[0,255]

        输出:,,三原色

        返回值:array($r,$g,$b)

        *********************************************************************/

        function,RandColor,($rs,$re,$gs,$ge,$bs,$be)

        {

        $r,=,mt_rand($rs,$re);

        $g,=,mt_rand($gs,$ge);

        $b,=,mt_rand($bs,$be);

        return,array($r,$g,$b);

        }

        /*********************************************************************

        函数名:CreateImage

        描述:,,创建验证码图片

        输出:,,验证码图片

        *********************************************************************/

        function,CreateImage,()

        {

        $im,=,imagecreate($this->img_w,$this->img_h);

        $bgcolor,=,imagecolorallocate($im,163,211,156);

        $black,=,imagecolorallocate($im,,0,0,0);

        $white,=,imagecolorallocate($im,,255,255,255);,

        /*

        scandir,--,列出指定路径中的文件和目录

        array,scandir,(,string,directory,[,,int,sorting_order,[,,resource,context]],)

        参数说明:

        directory:要被浏览的目录,

        sorting_order:默认的排序顺序是按字母升序排列。如果使用了可选参数,sorting_order(设为,1),则排序顺序是按字母降序排列。

        返回一个,array,包含有,directory,中的文件和目录。

        例:

        fonts文件夹与当前网页是同一个层次的,fonts下面包含一个simhei.ttf

        print_r(scandir("fonts/"));返回Array,(,[0],=>,.,[1],=>,..,[2],=>,simhei.ttf,)

        */

        $fonts,=,scandir($this->font_dir);,

        $fmax,=,count($fonts),-,2;,//排除Array[0]=>.和Array[1]=>..

        $ychar,=,$this->GetCodeChar($this->mode);

        /*

        explode,--,使用一个字符串分割另一个字符串

        array,explode,(,string,separator,,string,string,[,,int,limit],)

        参数说明:

        separator:分隔符

        string:被分割的字符串

        limit,:最多返回的数组元素个数

        返回由分隔符分割后的字符串组成的数组

        */

        $list,=,explode(",",$ychar);

        /*

        mt_rand,--,生成更好的随机数

        int,mt_rand,(,[int,min,,int,max],)

        */

        $x,=,mt_rand(2,$this->img_w/($this->v_num+2));

        $cmax,=,count($list),-,1;

        $v_code,=,'';

        for,($i=0;$i<$this->v_num;$i++), //验证码

        {

        $randnum,=,mt_rand(0,$cmax);

        , $this_char,=,$list[$randnum];

        , $v_code,.=,$this_char;

        , $size,=,mt_rand(intval($this->img_w/5),intval($this->img_w/4));,//获取字体大小

        , $angle,=,mt_rand(-20,20);,//获取角度

        , $y,=,mt_rand(($size+2),($this->img_h-2));,//设定了字体基线的位置,不是字符的最底端。

        ,,, $rand_color,=,$this->RandColor(0,200,0,100,0,250);

        ,,,

        $randcolor,=,imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);

        ,,, $fontrand,=,mt_rand(2,,$fmax);

        $font,=,"$this->font_dir/".$fonts[$fontrand];,//获取字体类型

        ,,, //imagettftext($im,,$size,,$angle,,$x,,$y,,$randcolor,,$font,,$this_char);

        imagettftext($im,,$size,,$angle,,$x,,$y,,$randcolor,,$font,,iconv("GB2312","UTF-8",$this_char));

        ,,, $x,=,$x,+,intval($this->img_w/($this->v_num+1));

        }

        for,($i=0;$i<$this->int_pixel_num;$i++),//干扰像素

        {

        ,,, $rand_color,=,$this->RandColor(50,250,0,250,50,250);

        ,,,

        $rand_color_pixel,=,imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);

        ,,,

        imagesetpixel($im,,mt_rand()%$this->img_w,,mt_rand()%$this->img_h,,$rand_color_pixel);

        }

        for,($i=0;$i<$this->int_line_num;$i++) //干扰线

        {,

        ,,, $rand_color,=,$this->RandColor(0,250,0,250,0,250);

        ,,,

        $rand_color_line,=,imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);

        ,,,

        imageline($im,,mt_rand(0,intval($this->img_w/3)),,mt_rand(0,$this->img_h),,mt_rand(intval($this->img_w,-,($this->img_w/3)),$this->img_w),,mt_rand(0,$this->img_h),,$rand_color_line);

        }

        imageantialias($im,true);,//抗锯齿

        $_SESSION[v_code],=,$v_code;,//用$_SESSION[v_code]保存验证码

        //生成图像

        if,(function_exists("imagegif")),

        {

        header,("Content-type:,image/gif");

        imagegif($im);

        }

        elseif,(function_exists("imagepng")),

        {

        header,("Content-type:,image/png");

        imagepng($im);

        }

        elseif,(function_exists("imagejpeg")),

        {

        header,("Content-type:,image/jpeg");

        imagejpeg($im);

        }

        elseif,(function_exists("imagewbmp")),

        {

        header,("Content-type:,image/vnd.wap.wbmp");

        imagewbmp($im);

        }

        else

        {

        die("本机不支持生成图像!");

        }

        imagedestroy($im);

        }

        }

        ?>

        index.php

        <?php

        session_start();

        if($_POST[submit]){

        if($_POST[v_code]!=$_SESSION[v_code]){

        echo,"<script>alert('验证码输入错误~');</script>";

        }else{

        echo,"<script>alert('验证码输入正确~');</script>";

        }

        }

        ?>

        <!DOCTYPE,html,PUBLIC,"-//W3C//DTD,XHTML,1.0,Transitional//EN","

        R/xhtml1/DTD/xhtml1-transitional.dtd">

        <html,xmlns=";>

        <head>

        <meta,http-equiv="Content-Type",content="text/html;,charset=gb2312",/> <title>验证码</title>

        <script,language="javascript">

        function,chkv_code(){

        if(document.getElementById("v_code").value==""){

        alert("请输入验证码~");

        document.getElementById("v_code").focus();

        return,false;

        }

        return,true;

        }

        </script>

        </head>

        <body>

        <form,method="post",onsubmit="return,chkv_code();"> <div,align="center">

        验证码:<input,type="text",name="v_code",size="8",maxlength="4">

        <img,src="verification_code.php",alt="看不清,点此刷新验证码

        ",name="verify_code",width="65",height="20",border="0",id="verify_code",onclick="document.

        getElementById('verify_code').src='verification_code.php?'+Math.random();",style="cursor:point

        er;",/>

        <p><input,type="submit",value="提&nbsp;交",name="submit"> </div>

        </form>

        </body>

        


怎样才能成为PHP高手PHP用户注册与登录
php调用linux计划任务php里session的用法
php在百度的发展历程php中关于引用(&)详解
PHP正则表达式的几则使用技巧 细说PHP模板引擎
windows下PHP运行环境安装详解js与php调用
PHP与ASP.NET的比较PHP基本语法补充案例
PHP简明教程php开源框架分析
PHP文件删除程序php阴历程序
信息发布:广州名易软件有限公司 http://www.myidp.net
  • 名易软件销售服务
  • 名易软件销售服务
  • 名易软件技术服务

  • 宝典php验证码制作