ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2020-03-17 11:40:16  阅读:252  来源: 互联网

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


创建一个验证码类

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

  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 '验证失败';
}

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

标签:rand,width,image,笔记,height,学习,mt,private,php
来源: https://blog.csdn.net/mayidream/article/details/104915268

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有