您的位置:首页 > 编程语言 > PHP开发

php学习笔记之小功能实现验证码类

2020-03-24 12:55 676 查看

创建一个验证码类

首先思考创建验证码的步骤:

  1. 创建文件夹名称为Vcode.class.php
  2. 创建成员变量
  3. 创建图像
  4. 设置图像边框颜色
  5. 设置像素点
  6. 设置干扰线
  7. 设置字体
  8. 释放资源
  9. 使用__toString()集成上面的方法(省去调用方法的步骤)
  10. 为了使功能更加专一,新建一个useVcode.php文件
  11. 制作form表单
  12. 判断用户传入的数据是否和设置的数据一致

一、创建成员变量

class Vcode
{
//成员方法
private $width;//设置验证码宽度
private $height;//设置验证码高度
private $image;//设置验证码对象
private $font;//设置字体
private $codeNum;//设置输出的字符数
}

二、成员方法

function __construct($width=100,$height=40,$codeNum=4)//构造方法,创建实例时自动调用,赋初值
{
//开启session
session_start(); //为了保存数据与用户输入的数据进行对比
$this->width=$width;
$this->height=$height;
$this->codeNum=$codeNum;
}

三、绘制图像

private function getImage(){
//创建画布
$this->image=imagecreatetruecolor($this->width,$this->height);
//填充画布颜色
$back=imagecolorallocate($this->image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
//填充背景
imagefill($this->image,0,0,$back);
//绘制边框背景颜色
$bordercolor=imagecolorallocate($this->image,255,0,0);
//为背景设置边框颜色
imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$bordercolor);
}

四、绘制像素点

private function setPixel(){

for ($i=0;$i<100;$i++){
//设置像素点颜色
$pixelcolor=imagecolorallocate($this->image,mt_rand(150,200),mt_rand(150,200),mt_rand(150,200));
//设置像素点
imagesetpixel($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),$pixelcolor);
}
}

五、设置干扰线

private function setLine(){
//设置线条颜色
for ($i=0;$i<=4;$i++){
$linecolor=imagecolorallocate($this->image,mt_rand(150,200),mt_rand(150,200),mt_rand(150,200));
imageline($this->image,mt_rand(0,$this->width/2-1),mt_rand(0,$this->height/2-1),mt_rand($this->width/2-1,$this->width),mt_rand($this->height/2-1,$this->height),$linecolor);
}
}

六、设置字符集

private function setChar(){
//传入字符串
$str='abcdefghigkLmaokqrstuvwxyz147258369';
//获取随机的四个字符
for ($i=0;$i<$this->codeNum;$i++){
$this->font.=$str{mt_rand(0,strlen($str)-1)};
}

for ($i=0;$i<strlen($this->font);$i++){
$fontColor=imagecolorallocate($this->image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
//设置字体左上角的Y点
$x=$this->width/$this->codeNum*$i+mt_rand(3,8);
$y=mt_rand(10,$this->height/2);
//写入字体
imagechar($this->image,mt_rand(3,5),$x,$y,$this->font{$i},$fontColor);
}
}

七、设置输出JPEG图像

private  function  outputImage(){
header('Content-type:image/jpeg');
imagejpeg($this->image);
}

八、释放资源

function __destruct(){
imagedestroy($this->image);

}

九、使用__toString()集成上面的方法(省去调用方法的步骤)

function __toString() //__toString方法不能使用private等关键词限制
{
//创建图像
$this->getImage();
//创建像素点
$this->setPixel();
//创建干扰线
$this->setLine();
//输出字体
$this->setChar();
//输出图像
$this->outputImage();
//保存会话给浏览器
$_SESSION['code']=$this->font;
return '';  //__toString()方法必须返回一个字符串类型
}

十、新建一个useVcode.php文件 ,实例化Vcode类

include_once './image.class.php'; //相对路径
echo new Vcode();

十一、 制作form表单

<form action="action.php" method="post">
验证码:<input type="text" name="code"><img src="./userImage.php" onclick="this.src='./userImage.php'"/>
<input type="submit" value="提交">

十二、 判断用户传入的数据是否和设置的数据一致

session_start();
var_dump($_SESSION['code']);//打印保存在code的数据
if (strtoupper($_POST['code'])==strtoupper($_SESSION['code'])){//strtoupper()方法将字符串转化为大写,因为是不区分大小写的
echo '验证成功';
}else{
echo '验证失败';
}

本人发表的所有文章仅为自己学习和复习使用,谢谢!

  • 点赞
  • 收藏
  • 分享
  • 文章举报
mayidream 发布了15 篇原创文章 · 获赞 4 · 访问量 185 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: