一个漂亮的php验证码类

时间:2025-10-06 08:50:17 php语言

一个漂亮的php验证码类

  编程的魅力在于可以实现想要的功能,下面小编就为大家分享一个漂亮的php验证码类。需要的朋友可以过来参考下,更多消息请关注应届毕业生网。

  直接上代码,代码如下:

  /pic/p>

  class ValidateCode {

  private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';/pic/p>

  private $code;/pic/p>

  private $codelen = 4;/pic/p>

  private $width = 130;/pic/p>

  private $height = 50;/pic/p>

  private $img;/pic/p>

  private $font;/pic/p>

  private $fontsize = 20;/pic/p>

  private $fontcolor;/pic/p>

  /pic/p>

  public function __construct() {

  $this->font = dirname(__FILE__).'/font/elephant.ttf';/pic/p>

  }

  /pic/p>

  private function createCode() {

  $_len = strlen($this->charset)-1;

  for ($i=0;$i<$this->codelen;$i++) {

  $this->code .= $this->charset[mt_rand(0,$_len)];

  }

  }

  /pic/p>

  private function createBg() {

  $this->img = imagecreatetruecolor($this->width, $this->height);

  $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));

  imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);

  }

  /pic/p>

  private function createFont() {

  $_x = $this->width / $this->codelen;

  for ($i=0;$i<$this->codelen;$i++) {

  $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));

  imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);

  }

  }

  /pic/p>

  private function createLine() {

  /pic/p>

  for ($i=0;$i<6;$i++) {

  $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));

  imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);

  }

  /pic/p>

  for ($i=0;$i<100;$i++) {

  $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));

  imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);

  }

  }

  /pic/p>

  private function outPut() {

  header('Content-type:image/png');

  imagepng($this->img);

  imagedestroy($this->img);

  }

  /pic/p>

  public function doimg() {

  $this->createBg();

  $this->createCode();

  $this->createLine();

  $this->createFont();

  $this->outPut();

  }

  /pic/p>

  public function getCode() {

  return strtolower($this->code);

  }

  }

  输出实例:

  使用方法:

  1、先把验证码类保存为一个名为 ValidateCode.class.php 的文件;

  2、新建一个名为 captcha.php 的文件进行调用该类;

  captcha.php

  代码如下:

  session_start();

  require './ValidateCode.class.php'; /pic/p>

  $_vc = new ValidateCode(); /pic/p>

  $_vc->doimg();

  $_SESSION['authnum_session'] = $_vc->getCode();/pic/p>

  3、引用到页面中,代码如下:

  代码如下:

  4、一个完整的验证页面,代码如下:

  代码如下:

  session_start();

  /pic/p>

  /pic/p>

  session_destroy();

  /pic/p>

  /pic/p>

  ?>

  此例为session验证实例

  验证码:

  /pic/p>

  /pic/p>

  ";

  $validate="";

  if(isset($_POST["validate"])){

  $validate=$_POST["validate"];

  echo "您刚才输入的是:".$_POST["validate"]."

  状态:";

  if($validate!=$_SESSION["authnum_session"]){

  /pic/p>

  echo "输入有误";

  }else{

  echo "通过验证";

  }

  }

  ?>

【一个漂亮的php验证码类】相关文章:

仿照TP框架自带的PHP验证码类12-29

php实现验证码制作12-24

php如何实现验证码03-15

php验证码代码怎么写08-30

用php生成带有雪花背景的验证码01-08

php生成动态图片验证码代码08-11

PHP学习:PHP拼音类01-25

PHP创建漂亮图表的步骤12-01

php如何在一个类中引入另外一个类02-13