zoukankan      html  css  js  c++  java
  • PHP封装 验证码

    <?php
    namespace Lib;
    class Captcha{
        private $width;
        private $height;
        public function __construct($width=80,$height=32) {
            $this->width=$width;
            $this->height=$height;
        }
        //生成随机字符串
        private function generalCode(){
            $all_array=array_merge(range('a','z'),range('A','Z'),range(0,9));    //所有字符数组
            $div_array=['1','l','0','o','O','I'];    //去除容易混淆的字符
            $array=array_diff($all_array,$div_array);    //剩余的字符数组
            unset($all_array,$div_array);        //销毁不需要使用的数组
            $index=array_rand($array,4);    //随机取4个字符,返回字符下标,按先后顺序排列
            shuffle($index);    //打乱字符
            $code='';
            foreach($index as $i)
                $code.=$array[$i];
            $_SESSION['code']=$code;        //保存到会话中
            return $code;
        }
        //创建验证码
        public function entry(){
            $code=$this->generalCode();
            $img=imagecreate($this->width, $this->height);
            imagecolorallocate($img,255,0,0);            //分配背景色
            $color=imagecolorallocate($img,255,255,255);    //分配前景色
            $font=5;        //内置5号字体
            $x=(imagesx($img)-imagefontwidth($font)*strlen($code))/2;
            $y=(imagesy($img)-imagefontheight($font))/2;
            imagestring($img,$font,$x,$y,$code,$color);
            //显示验证码
            header('content-type:image/gif');
            imagegif($img);
        }
        //验证码比较
        public function check($code){
            return strtoupper($code)== strtoupper($_SESSION['code']);
        }
    }
    ?>
  • 相关阅读:
    死磕 java线程系列之自己动手写一个线程池(续)
    Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用
    opencv之为图像添加边界
    协作,才能更好的中断线程
    Java并发——线程池Executor框架
    神经网络dropout
    xgboost
    物体检测-毕业设计项目回顾
    计算机网络-TCP连接
    gbdt推导和代码
  • 原文地址:https://www.cnblogs.com/SharkJiao/p/14165522.html
Copyright © 2011-2022 走看看