zoukankan      html  css  js  c++  java
  • John细说PHP的验证码

    细说php中的验证码类创建

    我这里自己写了一个验证码类,我来演示一下怎么使用,我是菜鸟一枚,大神请略过。我来讲解一下它的使用方法,总共需要两步即可。

    第一步:

    下载我制作好的验证码类。下载地址:https://files.cnblogs.com/files/xfjpeter/Verify.zip

    第二步:

     1.创建一个字的验证码文件

     1 <?php
     2 
     3 #引入验证码类文件
     4 require_once('Verify.class.php');
     5 
     6 #实例化验证码类
     7 #初始化的使用可以传四个参数,分别是:验证码图片的长、高,验证码的长度,验证码的类型(验证码的类型需要将bgRand属性设置为false)
     8 $code = new Verify(140, 40, 6, 6);
     9 
    10 #设置验证码图片的长度
    11 $code -> width = 200;
    12 
    13 #设置验证码图片的高度
    14 $code -> height = 60;
    15 
    16 #是否随机背景,默认true(随机)
    17 $code -> bgRand = false;
    18 
    19 #显示验证码
    20 $code -> verify();

     生成的图片样式为如图

    2.验证码类文件为

      1 <?php
      2 
      3 /**
      4  * 验证码类
      5  * @author John <fsyzxz@163.com>
      6  */
      7 class Verify
      8 {
      9     private $width = 160;       //验证码的宽度
     10     private $height = 60;       //验证码的高度
     11     private $type = 1;          //验证码的类型
     12     private $length = 4;        //验证码的长度
     13     private $code;              //验证码
     14     private $img;               //图像的资源
     15     private $seKey = 'John';    //密钥
     16     private $bgRand = true;     //随机背景图片
     17     
     18     /**
     19      * 构造函数
     20      * @param type $width 验证码的宽度
     21      * @param type $height 验证码的高度
     22      * @param type $length 验证码的长度
     23      * @param type $type 验证码的类型
     24      */
     25     public function __construct($width = 160, $height = 40, $length = 4, $type = 1)
     26     {
     27         $this->width  = !empty($width)  ? $width  : $this->width;
     28         $this->height = !empty($height) ? $height : $this->height;
     29         $this->length = !empty($length) ? $length : $this->length;
     30         $this->type   = !empty($type)   ? $type   : $this->type;
     31     }
     32     
     33     /**
     34      * 设置属性值
     35      * @param type $name 属性名
     36      * @param type $value 属性值
     37      */
     38     public function __set($name, $value)
     39     {
     40         if (isset($name)) {
     41             $this->$name = $value;
     42         }
     43     }
     44     
     45     /**
     46      * 获取属性值
     47      * @param type $name 属性名
     48      * @return type 返回属性值
     49      */
     50     public function __get($name) {
     51         return $this->$name;
     52     }
     53     
     54     /**
     55      * 校验验证码
     56      * @param type $code 表单提供的验证码
     57      * @return boolean
     58      */
     59     public function check($code){
     60         if (!isset($_SESSION)) {session_start();}
     61         if ($this->encodeVerify(strtolower($code)) === $_SESSION['code']){
     62             return true;
     63         }else{
     64             return false;
     65         }
     66     }
     67     
     68     //输出验证码
     69     public function verify()
     70     {
     71         $this->code = $this->createVerify();
     72         //创建背景
     73         $this->createBackground();
     74         //文字显示
     75         $this->writeString();
     76         //画干扰线
     77         $this->paitLine();
     78         //输入图像
     79         $this->printImg();
     80     }
     81     
     82     /**
     83      * 创建背景图片
     84      */
     85     private function createBackground()
     86     {
     87         //从图片库创建一个图像, 判断是否随机
     88         if ($this->bgRand){
     89             $img = imagecreatefromjpeg('./verify/bgs/'.mt_rand(1,8).'.jpg');
     90         }else{
     91             $img = imagecreatefromjpeg('./verify/bgs/'.$this->type.'.jpg');
     92         }
     93         //创建一个图片
     94         $this->img = imagecreatetruecolor($this->width, $this->height);
     95         //把图片复制到创建的图像上
     96         imagecopyresampled($this->img, $img, 0, 0, 0, 0, $this->width, $this->height, imagesx($img), imagesy($img));
     97     }
     98     
     99     /**
    100      * 在图片上写字
    101      */
    102     private function writeString()
    103     {
    104         $color = imagecolorallocatealpha($this->img, mt_rand(0,128), mt_rand(0,128), mt_rand(0,128), 0);
    105         $fontType = './verify/ttfs/'.mt_rand(1,6).'.ttf';
    106         $fontSize = mt_rand(15, 20);
    107         for ($i = 0; $i < $this->length; $i++) {
    108             $x = 3+($this->width/$this->length)*$i;
    109             $y = mt_rand(($this->height/3)*2, ($this->height/3)*2);
    110             //把验证码写在图片上
    111             imagettftext($this->img, $fontSize, 0, $x, $y, $color, $fontType, $this->code[$i]);
    112         }
    113     }
    114     
    115     /**
    116      * 画干扰线和字母
    117      */
    118     private function paitLine()
    119     {
    120         $px = $py = 0;
    121         $codes = '2345678abcdefhijkmnpqrstuvwxyz';
    122         for ($i = 0; $i < $this->width/4; $i++){
    123             $num = mt_rand(0, strlen($codes)-1);
    124             $color = imagecolorallocatealpha($this->img, 255, 255, 255, 80);
    125             //画字母
    126             imagechar($this->img, 8, mt_rand(3, $this->width), mt_rand(3, $this->height), $codes{$num}, $color);
    127         }
    128     }
    129     
    130     /**
    131      * 输入图像
    132      */
    133     private function printImg()
    134     {
    135         if(function_exists('imagegif')){
    136             // 针对 GIF
    137             header('Content-Type: image/gif');
    138             imagegif($this->img);
    139         }elseif(function_exists('imagejpeg')){
    140             // 针对 JPEG
    141             header('Content-Type: image/jpeg');
    142             imagejpeg($this->img, NULL, 100);
    143         }elseif(function_exists('imagepng')){
    144             // 针对 PNG
    145             header('Content-Type: image/png');
    146             imagepng($this->img);
    147         }elseif(function_exists('imagewbmp')){
    148             // 针对 WBMP
    149             header('Content-Type: image/vnd.wap.wbmp');
    150             imagewbmp($this->img);
    151         }
    152     }
    153     
    154     /**
    155      * 生成验证码
    156      * @return string 返回生成的验证码
    157      */
    158     private function createVerify()
    159     {
    160         $codeSet = '2345678abcdefhijkmnpqrstuvwxyz';
    161         $codes = '';
    162         for ($i = 0; $i < $this->length; $i++) {
    163             $codes .= $codeSet[mt_rand(0, strlen($codeSet)-1)];
    164         }
    165         //把验证码保存到session中
    166         if (!isset($_SESSION)) {session_start();}
    167         $_SESSION['code'] = $this->encodeVerify(strtolower($codes));
    168 //        $_SESSION['code'] = $codes;
    169         return $codes;
    170     }
    171     
    172     /**
    173      * 加密验证码
    174      * @param type $string
    175      * @return type
    176      */
    177     private function encodeVerify($string)
    178     {
    179         $key = substr(md5($this->seKey), 5, 8);
    180         $str = substr(md5($string), 8, 10);
    181         return md5($key . $str);
    182     }
    183     
    184     /**
    185      * 销毁图像
    186      */
    187     function __destruct()
    188     {
    189         if (isset($this->img)){
    190             imagedestroy($this->img);
    191         }
    192     }
    193 }

     以上两步即可生生你想要的验证。

    另外说明,Verify.class.php中有一个验证验证码是否正确的方法,使用如下

    将你从界面中获得的验证码传入code方法中即可

    if ($code -> code(这是传入你页面中获取的验证码值)){
        #这是验证正确的操作
    }else{
        #验证失败的操作
    }

    以上就是我创建整个验证码的心得,希望对点击进来看的人有帮助。

    I need you ideas!
  • 相关阅读:
    Python基础学习
    My First Bog
    WPF 自定义窗口,自定义控件和样式
    WPF 数据规则验证
    C# 类的扩展方法
    C# 类的序列化和反序列化
    数据库使用空间查询方法
    关于linux环境下django获取中文url报错处理
    Django中关于csrf_token的认证
    Django + Uwsgi + Nginx 的生产环境部署
  • 原文地址:https://www.cnblogs.com/xfjpeter/p/5813943.html
Copyright © 2011-2022 走看看