首页 > 作文

PHP code 验证码生成类定义和简单使用示例

更新时间:2023-04-08 17:59:51 阅读: 评论:0

本文实例讲述了php code 验证码生成类定义和简单使用。分享给大家供大家参考,具体如下:

code.php

<?phpnamespace code;/** * class code */class code{  protected $number;//验证码内字符个数  protected $codetype;//验证码样式  protected $width;//图像宽  protected $height;//图像高  protected $code;//验证码  protected $image;//图像资源   /**   * code constructor.   * @param int $number   * @param int $codetype   * @param int $width   * @param int $height   */  public function __construct($number=5, $codetype=2, $width=100, $height=40)  {    $this->number = $number;    $this->codetype = $codetype;    $this->width = $width;    $this->height = $height;    $this->code = $this->createcode();  }   /**   * 销毁资源   */  public function __destruct()  {    imagedestroy($this->image);  }   /**   * 外部调用code时触发   * @param $name   * @return bool   */  public function __get($name)  {    if ('code' == $name) {      return $this->$name;    } el {      return fal;    }  }   /**   * 生成code   */  protected function createcode()  {    switch ($this->codetype) {      ca 0:        $code = $this->getnum();        break;      ca 1:        $code = $this->getchar();        break小学五年级试卷分析;      ca 2:        $code = $this->getnumchar();        break;      default:        die('样式不对');    }    return $code;  }   /**   * 数字验证码   * @return string   */  protected function getnum()  {    $str = join('', range(0,9));    return substr(str_shuffle($str), 0, $this->number);  }   /**   * 字符验证码   * @return string   */  protected function getchar()  {    $str = join('', range('a', 'z'));    $str = $str . strtoupper($str);    return substr(str_shuffle($str), 0, $this->number);  }   /**   * 字符和数字混合验证码   * @return string   */  protected function getnumchar()  {    $num = join('', range(0, 9));    $str = join('', range('a', 'z'));    $str_big = strtoupper($str);    $numchar = $num . $str . $str_big;    return substr(str_shuffle($numchar), 0, $this->number);  }   /**   * 生成图像   */  protected function createimage()  {    $this->image = imagecreatetruecolor($this->width, $this->height);  }   /**   * 填充背景色   */  protected function fillcolor()  {    imagefill($this->image, 0, 0, $this->lightcolor());  }   /**   * 浅颜色   * @return int   */  protected function lightcolor()  {    return imagecolorallocate($this->image, mt_rand(170, 255), mt_rand(170, 255), mt_rand(170, 255));  }   /**   * 深颜色   * @return int   */  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 - 5));      $y = mt_rand(0, $this->he天孤星ight - 15);      imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkcolor());    }  }   /**   * 添加干扰点   */  protected function drawd滤纸isturb()  {    for ($i= 0; $i < 100怎么下载视频到电脑里; $i++) {      imagetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), $this->darkcolor());    }  }   /**   * 添加干扰线   */  protected function drawarc()  {    for ($i = 0; $i < $this->number - 3; $i++) {      imagearc($this->image, mt_rand(5, $this->width), mt_rand(5, $this->height), mt_rand(5, $this->width), mt_rand(5, $this->height),mt_rand(0, 70), mt_rand(300, 360), $this->darkcolor());    }  }   /**   * 输出显示   */  protected function show()  {    header('content-type:image/png');    imagepng($this->image);  }   /**   * 外部image   */  public function outimage()  {    $this->createimage();//创建画布    $this->fillcolor();//填充背景色    $this->drawchar();//添加验证字符    $this->drawdisturb();//添加干扰点    $this->drawarzr元素c();//添加干扰线    $this->show();//输出  }}

展示验证码。。保存验证码和过期时间

<?phpinclude './code/code.php'; $code = new code\code();$code->outimage();ssion_start();$_ssion['code'] = [  'code' => $code->code,  'exp_time' => time() + (60 * 60 * 10),];

本文发布于:2023-04-08 17:59:49,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/59bf57bdb95a19b090472971022b54bc.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP code 验证码生成类定义和简单使用示例.doc

本文 PDF 下载地址:PHP code 验证码生成类定义和简单使用示例.pdf

标签:验证码   字符   干扰   图像
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图