验证码类

验证码类

<?php
class 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

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

引用 code

<?php
header('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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值