网络编程 
首页 > 网络编程 > 浏览文章

PHP实现简单实用的验证码类

(编辑:jimmy 日期: 2024/10/15 浏览:3 次 )

本文实例讲述了PHP实现简单实用的验证码类。分享给大家供大家参考。具体如下:

<"Content-type: image/png");
}
/**
  * 产生验证码
  */
private function createCode()
{
  $this->checkcode = strtoupper(substr(md5(rand()),0,$this->codenum));
}
/**
  * 产生验证码图片
  */
private function createImage()
{
  $this->checkimage = @imagecreate($this->width,$this->height);
  $back = imagecolorallocate($this->checkimage,255,255,255); 
  $border = imagecolorallocate($this->checkimage,0,0,0); 
  imagefilledrectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$back); // 白色底
  imagerectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$border);  // 黑色边框
}
/**
  * 设置图片的干扰像素 
  */
private function setDisturbColor()
{
  for ($i=0;$i<=200;$i++)
  {
  $this->disturbColor = imagecolorallocate($this->checkimage, rand(0,255), rand(0,255), rand(0,255));
  imagesetpixel($this->checkimage,rand(2,128),rand(2,38),$this->disturbColor);
  }
}
/**
  *
  * 在验证码图片上逐个画上验证码
  *
  */
private function writeCheckCodeToImage()
{
  for ($i=0;$i<$this->codenum;$i++)
  {
  $bg_color = imagecolorallocate ($this->checkimage, rand(0,255), rand(0,128), rand(0,255));
  $x = floor($this->width/$this->codenum)*$i;
  $y = rand(0,$this->height-15);
  imagechar ($this->checkimage, rand(5,8), $x+5, $y, $this->checkcode[$i], $bg_color);
  }
}
function __destruct()
{
  unset($this->width,$this->height,$this->codenum,$this->session_flag);
}
}
?>

希望本文所述对大家的php程序设计有所帮助。

上一篇:php计算title标题相似比的方法
下一篇:php使用gzip压缩传输js和css文件的方法
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?