一个实用的php验证码类


Posted in PHP onJuly 06, 2017

万能php验证码类,供大家参考,具体内容如下

code.php是验证码类,类的名称最好和文件名的名称一样,这样有利于我们的查看。

code.php

<?php
header('Content-type:text/html;charset=utf8');
class Code{
  // 验证码个数$number
  protected $number;
  // 验证码类型$codeType
  protected $codeType;
  // 验证码图像宽度$width
  protected $width;
  // 验证码$height
  protected $height;
  // 验证码字符串$code
  protected $code;
  // 图像资源$image
  protected $image;
  
  public function __construct($number=4,$codeType=0,$height=50,$width=100){
    //初始化自己的成员属性
    $this->number=$number;
    $this->codeType=$codeType;
    $this->width = $width;
    $this->height= $height;
    
    //生成验证码函数
    $this->code = $this ->createCode();
    
  }
  public function __get($name){
    if ($name == 'code'){
      return $this->code;
    }
    return false;
  }
  /*获取验证码*/
  public function getCode() {
    return $this->code;
  }
  /*图像资源销毁*/
  public function __destruct(){
    imagedestroy($this->image);
  }
  protected function createCode(){
    //通过你的验证码类型生成验证码
    switch ($this->codeType){
      case 0: //纯数字
        $code = $this->getNumberCode();
        break;
      case 1: //纯字母的
        $code = $this->getCharCode();
        break;
      case 2: //数字和字母混合
        $code = $this->getNumCharCode();
        break;
      default:
        die('不支持此类验证码类型');
    }
    return $code;
  }
  protected function getNumberCode(){
    $str = join('', range(0, 9));
    return substr(str_shuffle($str),0, $this->number);
  }
  protected function getCharCode(){
    $str = join('', range('a', 'z'));
    $str = $str.strtoupper($str);
    return substr(str_shuffle($str),0,$this->number);
  }
  protected function getNumCharCode(){
    $numstr = join('',range(0, 9));
    $str =join('', range('a', 'z'));
    $str =$numstr.$str.strtoupper($str);
    return substr(str_shuffle($str), 0,$this->number);
  }
  protected function createImage(){
    $this->image = imagecreatetruecolor($this->width, 
        $this->height);
  }
  protected function fillBack(){
    imagefill($this->image, 0, 0, $this->lightColor());
  }
  /*浅色*/
  protected function lightColor(){
    return imagecolorallocate($this->image, mt_rand(133,255), mt_rand(133,255), mt_rand(133,255));
  }
  /*深色*/
  protected function darkColor(){
    return imagecolorallocate($this->image, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120));
  }
  protected function drawChar(){
    $width = ceil($this->width / $this->number);
    for ($i=0; $i< $this->number;$i++){
      $x = mt_rand($i*$width+5, ($i+1)*$width-10);
      $y = mt_rand(0, $this->height -15);
      imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor());
    }
  }
  protected function drawLine(){
    for ($i=0;$i<5;$i++) {
      imageline($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$this->darkColor());
    }
  }
  protected function drawDisturb(){
    for ($i=0;$i<150;$i++){
      $x=mt_rand(0, $this->width);
      $y=mt_rand(0, $this->height);
      imagesetpixel($this->image, $x, $y, $this->lightColor());
    }
  }
  protected function show(){
    header('Content-Type:image/png');
    imagepng($this->image);
  }
  public function outImage(){
//     创建画布
    $this->createImage();
//     填充背景色
    $this->fillBack();
//     将验证码字符串花到画布上
    $this->drawChar();
//     添加干扰元素
    $this->drawDisturb();
//     添加线条
    $this->drawLine();
//     输出并显示
    $this->show();
  }
}

test.php是new一个新的验证码,并把它保存到session中,为我们验证码的验证起到保存和存储的作用。

test.php

<?php
//开启session
session_start();
require_once 'code.php';

$code= new Code(4,1,50,100);
$_SESSION['code']= $code->getCode();
$code->outImage();

login.php就是最后的验证。

login.php

<?php 
    //开启Session 
    session_start(); 
    //判断是否提交 
    if(isset($_POST['dosubmit'])){ 
      //获取session中的验证码并转为小写 
      $sessionCode=strtolower($_SESSION['code']); 
      //获取输入的验证码 
      $code=strtolower($_POST['code']); 
      //判断是否相等 
      if($sessionCode==$code){ 
        echo "<script type='text/javascript'>alert('验证码正确!');</script>"; 
      }else{ 
        echo "<script type='text/javascript'>alert('验证码错误!');</script>"; 
      } 
    } 
  ?> 
  <!DOCTYPE html> 
  <html> 
    <head> 
      <title></title> 
      <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> 
      <style type="text/css"> 
        *{margin:0px;padding:0px;} 
        ul{ 
          width:400px; 
          list-style:none; 
          margin:50px auto; 
        } 
         
        li{ 
          padding:12px; 
          position:relative; 
        } 
         
        label{ 
          width:80px; 
          display:inline-block; 
          float:left; 
          line-height:30px; 
        } 
         
        input[type='text'],input[type='password']{ 
          height:30px; 
        } 
         
        img{ 
          margin-left:10px; 
        } 
         
        input[type="submit"]{ 
          margin-left:80px; 
          padding:5px 10px; 
        } 
      </style> 
    </head> 
    <body> 
      <form action="login.php" method="post"> 
        <ul> 
          <li> 
            <label>用户名:</label> 
            <input type="text" name="username"/> 
          </li> 
          <li> 
            <label>密码:</label> 
            <input type="password" name="password"/> 
          </li> 
          <li> 
            <label>验证码:</label> 
            <input type="text" name="code" size="4" style="float:left"/> 
            <img src="test.php" onclick="this.src='test.php?Math.random()'"/> 
          </li> 
          <li> 
            <input type="submit" value="登录" name="dosubmit"/> 
          </li> 
        </ul> 
      </form> 
    </body> 
  </html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
最省空间的计数器
Oct 09 PHP
深入php之规范编程命名小结
May 15 PHP
ini_set的用法介绍
Jan 07 PHP
PHP中nowdoc和heredoc使用需要注意的一点
Mar 21 PHP
神盾加密解密教程(二)PHP 神盾解密
Jun 08 PHP
PHP实现CSV文件的导入和导出类
Mar 24 PHP
调试WordPress中定时任务的相关PHP脚本示例
Dec 10 PHP
php redis实现文章发布系统(用户投票系统)
Mar 04 PHP
PHP中关键字interface和implements详解
Jun 14 PHP
PHP中模糊查询并关联三个select框
Jun 19 PHP
PHP-X系列教程之内置函数的使用示例
Oct 16 PHP
PHP哈希表实现算法原理解析
Dec 11 PHP
万能的php分页类
Jul 06 #PHP
PHP 实现从数据库导出到.csv文件方法
Jul 06 #PHP
php文件上传类的分享
Jul 06 #PHP
PHP图片水印类的封装
Jul 06 #PHP
php生出随机字符串
Jul 06 #PHP
PHP实现的分页类定义与用法示例
Jul 05 #PHP
PHP实现的文件上传类与用法详解
Jul 05 #PHP
You might like
提问的智慧
2006/10/09 PHP
php auth_http类库进行身份效验
2009/03/19 PHP
thinkphp5.1框架模板布局与模板继承用法分析
2019/07/19 PHP
javascript 防止刷新,后退,关闭
2010/08/07 Javascript
防止浏览器记住用户名及密码的简单实用方法
2013/04/22 Javascript
js图片延迟技术一般的思路与示例
2014/03/20 Javascript
24款热门实用的jQuery插件推荐
2014/12/24 Javascript
jQuery中contents()方法用法实例
2015/01/08 Javascript
jQuery添加和删除输入文本框标签代码
2016/05/20 Javascript
js 截取或者替换字符串中的数字实现方法
2016/06/13 Javascript
JavaScript拖动层Div代码
2017/03/01 Javascript
微信小程序wx:for和wx:for-item的用法详解
2018/04/01 Javascript
vue 之 css module的使用方法
2018/12/04 Javascript
微信小程序实现底部弹出框
2020/11/18 Javascript
Windows下用py2exe将Python程序打包成exe程序的教程
2015/04/08 Python
对python多线程SSH登录并发脚本详解
2019/02/14 Python
python使用pip安装SciPy、SymPy、matplotlib教程
2019/11/20 Python
python 二维矩阵转三维矩阵示例
2019/11/30 Python
Django后端发送小程序微信模板消息示例(服务通知)
2019/12/17 Python
详解numpy.ndarray.reshape()函数的参数问题
2020/10/13 Python
html5 Canvas画图教程(7)—canvas里画曲线之quadraticCurveTo方法
2013/01/09 HTML / CSS
雅诗兰黛美国官网:Estee Lauder美国
2016/07/21 全球购物
英国领先的电子、技术和办公用品购物网站:Ebuyer
2018/04/04 全球购物
伦敦鲜花递送:Flower Station
2021/02/03 全球购物
介绍一下linux的文件系统
2012/03/20 面试题
Linux内核产生并发的原因
2016/11/08 面试题
Python文件操作的面试题
2013/06/22 面试题
计算机工程学院个人求职信
2013/10/05 职场文书
夜班门卫岗位职责
2013/12/09 职场文书
施工资料员岗位职责
2014/01/06 职场文书
大三毕业自我鉴定
2014/01/15 职场文书
优秀公益广告词大全
2014/03/19 职场文书
亮剑精神演讲稿
2014/05/23 职场文书
单位委托书范本(3篇)
2014/09/18 职场文书
社保转移委托书范本
2014/10/08 职场文书
2016反腐倡廉警示教育心得体会
2016/01/13 职场文书