php小功能,php学习笔记之小功能实现1

创建一个验证码类

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

创建文件夹名称为Vcode.class.php

创建成员变量

创建图像

设置图像边框颜色

设置像素点

设置干扰线

设置字体

释放资源

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

为了使功能更加专一,新建一个useVcode.php文件

制作form表单

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

一、创建成员变量

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;$icodeNum;$i++){

$this->font.=$str{mt_rand(0,strlen($str)-1)};

}

for ($i=0;$ifont);$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表单

验证码:userImage.php

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

session_start();

var_dump($_SESSION['code']);//打印保存在code的数据

if (strtoupper($_POST['code'])==strtoupper($_SESSION['code'])){//strtoupper()方法将字符串转化为大写,因为是不区分大小写的

echo '验证成功';

}else{

echo '验证失败';

}

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

标签:rand,width,image,笔记,height,学习,mt,private,php

来源: https://blog.csdn.net/mayidream/article/details/104915268

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值