资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

PHP验证码 高洛峰细说PHP

前端页面index.php

创新互联建站坚持“要么做到,要么别承诺”的工作理念,服务领域包括:做网站、网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的绵竹网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

';
    }else{
        echo '输入不对
';     } } ?> 用户名:
标题:
内容:
验证码:

生成验证码图片test.php

服务器自己的空间保存一份
        $_SESSION['code'] = $vcode->getCode();
        //将验证码的图片输出
        $vcode->outimg();//调用方法

验证码类 print.php

width        =   $width;
                $this->heigth       =   $height;
                $this->num          =   $num;
                $this->code        =   $this->createCode();
        }
        //获取字符的验证码
        function getCode(){
            return $this->code;
        }
        
        
        //输出验证码图形
        function outimg(){
            //创建背景 颜色 大小 边框
            $this->createBack();           
            //画字 大小 字体颜色
            $this->outString();
            //干扰元素 点 线条
            $this->setDisturb();
            //输出图像
            $this->printImg();
        }
        //创建背景
        private function createBack(){
            //创建资源
            $this->img = p_w_picpathcreatetruecolor($this->width, $this->heigth);
            //设置随机背景颜色
            $bgcolor = p_w_picpathcolorallocate($this->img, rand(225, 255), rand(225, 255), rand(225, 255));
            //填充背景色
            p_w_picpathfill($this->img, 0, 0, $bgcolor);
            //画矩形
            $bordercolor = p_w_picpathcolorallocate($this->img, 0, 0, 0);
            p_w_picpathrectangle($this->img, 0, 0, $this->width-1, $this->heigth-1, $bordercolor);
        }
        //画字
        private function  outString(){
                for($i=0;$i<$this->num;$i++){                        
                $color  =   p_w_picpathcolorallocate($this->img, rand(0, 128), rand(0, 128), rand(0, 128));
                $font = rand(3,5);
                $x = 3 + ($this->width/$this->num)*$i;
                $y = rand(1, 5);
                p_w_picpathstring($this->img, $font,$x, $y, $this->code{$i}, $color);
                           }
        }
        //设置干扰元素
        private function setDisturb(){
                //加上点数
                for($i=0;$i<100;$i++){
                    $color  =   p_w_picpathcolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));
                    p_w_picpathsetpixel($this->img, rand(1, $this->width-2), rand(1, $this->heigth-2), $color);
                }
                //加上线条
                for($i=0;$i<10;$i++){
                    $color  =   p_w_picpathcolorallocate($this->img, rand(0, 255), rand(0, 128), rand(0, 255));
                    p_w_picpatharc($this->img, rand(-10, $this->width+10), rand(-10, $this->heigth+10), rand(30, 300), rand(30, 300), 55, 44, $color);
                }
        }
        //输出图像
        private function printImg(){
          //      header("Content-Type:p_w_picpath/jpeg");
           //     p_w_picpathjpeg($this->img);
           if(p_w_picpathtypes() & IMG_GIF){
                  header("Content-Type:p_w_picpath/gif");
                 p_w_picpathjpeg($this->img);
           }elseif(p_w_picpathtypes() & IMG_JPEG){
                  header("Content-Type:p_w_picpath/jpeg");
                 p_w_picpathjpeg($this->img);
           }elseif(p_w_picpathtypes() & IMG_JPG){
                  header("Content-Type:p_w_picpath/jpg");
                 p_w_picpathjpeg($this->img);
           }elseif(p_w_picpathtypes() & IMG_PNG){
                  header("Content-Type:p_w_picpath/png");
                 p_w_picpathjpeg($this->img);
           }
        }
        //生成验证码
        private function  createCode(){
            $codes = "23456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
            $code = "";
            for($i=0;$i<$this->num;$i++){
                $code .=$codes{rand(0,strlen($codes)-1)};
            }
            return $code;
        }
        
        //释放图像资源
        function __destruct(){
            p_w_picpathdestroy($this->img);
        }
        
    }


网站栏目:PHP验证码 高洛峰细说PHP
当前地址:http://cdkjz.cn/article/ppsjii.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220