栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP 验证码

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP 验证码

前端页面index.php

header('content-type:text/html;charset=utf-8');

if(isset($_POST['dosubmit'])){

    session_start();

    if(strtoupper($_SESSION['code']) == strtoupper($_POST['code'])){

        echo '输入成功!
';

    }else{

        echo '输入不对
';

    }

}

?>

生成验证码图片test.php

       //开启session

       session_start();

        require 'print.php';//导入验证码类文件

        $vcode =new Vcode(80,30,4);//实例化验证码类

        //将验证码放到服务器自己的空间保存一份

        $_SESSION['code'] = $vcode->getCode();

        //将验证码的图片输出

        $vcode->outimg();//调用方法

验证码类 print.php

    class Vcode{

        private $width;         //宽

        private $heigth;        //高

        private $num;           //数量

        private $code;          //验证码

        private $img;           //图像资源

        //构造方法

        function __construct($width=80,$height=25,$num=4){

                $this->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 = imagecreatetruecolor($this->width, $this->heigth);

            //设置随机背景颜色

            $bgcolor = imagecolorallocate($this->img, rand(225, 255), rand(225, 255), rand(225, 255));

            //填充背景色

            imagefill($this->img, 0, 0, $bgcolor);

            //画矩形

            $bordercolor = imagecolorallocate($this->img, 0, 0, 0);

            imagerectangle($this->img, 0, 0, $this->width-1, $this->heigth-1, $bordercolor);

        }

        //画字

        private function  outString(){

                for($i=0;$i<$this->num;$i++){                        

                $color  =   imagecolorallocate($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);

                imagestring($this->img, $font,$x, $y, $this->code{$i}, $color);

                           }

        }

        //设置干扰元素

        private function setDisturb(){

                //加上点数

                for($i=0;$i<100;$i++){

                    $color  =   imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));

                    imagesetpixel($this->img, rand(1, $this->width-2), rand(1, $this->heigth-2), $color);

                }

                //加上线条

                for($i=0;$i<10;$i++){

                    $color  =   imagecolorallocate($this->img, rand(0, 255), rand(0, 128), rand(0, 255));

                    imagearc($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:image/jpeg");

           //     imagejpeg($this->img);

           if(imagetypes() & IMG_GIF){

                  header("Content-Type:image/gif");

                 imagejpeg($this->img);

           }elseif(imagetypes() & IMG_JPEG){

                  header("Content-Type:image/jpeg");

                 imagejpeg($this->img);

           }elseif(imagetypes() & IMG_JPG){

                  header("Content-Type:image/jpg");

                 imagejpeg($this->img);

           }elseif(imagetypes() & IMG_PNG){

                  header("Content-Type:image/png");

                 imagejpeg($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(){

            imagedestroy($this->img);

        }

         

    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/230035.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号