验证码类

来源:互联网 发布:极客学院大数据百度云 编辑:程序博客网 时间:2024/05/29 03:21

验证码类

<?phpclass captcha{    private $code ="qwertyuioplkjhgfdsazxcvbnmMNBVCXZASDFGHJKLPOIUYTREWQ1234567890";    private $whide;    private $height;    private $code_num;    private $select_num;    private $language;    private $font;    function __construct($language='Chinnese',$whide=100,$height=30,$num=4){    $this->language=$language;    $this->whide=$whide;    $this->height=$height;    $this->code_num=$num;    $this->font = './111.ttf';    $this->get_cod();    }    //兼容    function captcha($language,$whide,$height,$num){        $this->__construct($language,$whide,$height,$num);    }    //验证码图片    function get_img(){        $img = imagecreatetruecolor($this->whide,$this->height);        $bgcolor= imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));        imagefill($img,0,0,$bgcolor);        //画一些障碍物        $this->draw($img);        //输出 code        $this->code($img);        imagepng($img);        imagedestroy($img);    }    //画一些障碍物    function draw($img){        //画点        for ($i=0; $i<50;$i++){            $print_color = imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200));            imagesetpixel($img,rand(0,$this->whide),rand(0,$this->height),$print_color);        }        //划线        for ($i=0; $i<10;$i++){            $line_color = imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200));            imageline($img,rand(0,$this->whide),rand(0,$this->height),rand(0,$this->whide),rand(0,$this->height),$line_color);        }    }    //验证码实体    function code($img){        $color = imagecolorallocate($img,rand(20,100),rand(20,100),rand(20,100));            for ($i = 0; $i < $this->code_num; $i++) {                if ($this->language == 'English') {                imagestring($img, 5, 15 + 20 * $i, rand(0, 15), $this->select_num[$i], $color);            }else{                    echo 111;                    imagettftext($img,14,6,10,rand(0, 15),$color,$this->font,mb_substr($this->select_num,$i,1));            }        }    }    function get_cod(){        $str='';        if ($this->language == 'English'){            for ($i=0 ;$i<$this->code_num;$i++){                $str.=$this->code{rand(0,strlen($this->code)-1)};            }        }else{           $text = file_get_contents('./test.txt');           while(1){               $i = mt_rand(0,mb_strlen($text, 'utf8'));               $str .= mb_substr($text,$i,1, 'utf8');               if (mb_strlen($str,'utf8')== $this->code_num){                   break;               }           }        }        session_start();        $_SESSION['codes'] = $str;        $this->select_num = $str;    }}

html

<html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport"          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>验证码验证</title>    <style>        form{            display: flex;            align-items: center;        }    </style></head><body><form action="index.php" method="post">    <img id="img" src="code.php" alt="" style="cursor:pointer"><span id="btn" style="cursor:pointer">看不清换一张!</span>    <input type="text" name="code">    <input type="submit" value="提交"></form></body></html><script>    var img = document.getElementById('img');    var btn = document.getElementById('btn');    btn.onclick=function(){        img.src="code.php";    }    img.onclick=function(){        img.src="code.php";    }</script>

引用 php

<?phpheader('content-type:text/html;charset=utf8');session_start();var_dump($_POST['code'] == $_SESSION['codes']);if ($_POST['code'] == $_SESSION['codes']){    exit('<script>alert("验证码正确!!")</script>');}

引用 code

<?phpheader('content-type:text/html;charset=utf8');header('content-type:image/png;');require  './captcha.php';$captcha = new captcha('English',100,30,4,111);$captcha->get_img();
原创粉丝点击