用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2265|回复: 0
收起左侧

验证码问题

[复制链接]
发表于 2012-7-16 21:32:04 | 显示全部楼层 |阅读模式
自己网上弄了个验证码放在libraries下,命名为ValidationCode.php
PHP复制代码
 
<?php
 class ValidationCode {
  private $width, $height, $codenum;  //验证码宽度、高度、位数
  public $code;  //产生的验证码
  private $codeimage;  //验证码图片
  private $distrubcolor;  //干扰像素
 
  //析构函数,设置验证码宽度、高度、位数
  function __construct($width = '80', $height = '20', $codenum = '4') {
   $this->width = $width;
   $this->height = $height;
   $this->codenum = $codenum;
  }
 
  //输出验证码图片
  public function outImage() {
   $this->outFileHeader();
   $this->createCode();
   $this->createImage();
   $this->setDistrubColor();
   $this->writeCodeImage();
   imagepng($this->codeimage);
   imagedestroy($this->codeimage);
  }
 
  //验证码输出头
  public function outFileHeader() {
   header('Content-type: image/png');
  }
 
  //产生验证码
  public function createCode() {
   $this->code = strtoupper(substr(md5(rand()), 0, $this->codenum));
  }
 
  //产生验证码图片
  public function createImage() {
   $this->codeimage = @imagecreate($this->width, $this->height);  //新建一个基于调色板的图像
   imagecolorallocate($this->codeimage, 200, 200, 200);  //为新建的图像分配颜色  
  }
 
  //设置图片干扰像素
  public function setDistrubColor() {
   for ($i = 0; $i < 128; $i++) {
    $this->distrubcolor = imagecolorallocate($this->codeimage, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($this->codeimage, rand(2, 128), rand(2, 38), $this->distrubcolor);  //画上干扰点
   }
  }
 
  //在背景上逐个画上验证码
  public function writeCodeImage() {
   for ($i = 0; $i < $this->codenum; $i++) {
    $bg_color = imagecolorallocate($this->codeimage, rand(0, 255), rand(0, 128), rand(0, 255));
    $x = floor($this->width / $this->codenum) * $i;
    $y = rand(0, $this->height - 15);
    imagechar($this->codeimage, 5, $x, $y, $this->code[$i], $bg_color);
   }
  }
 
 }
 
 
复制代码

在controllers中引用
PHP复制代码
 
function authcode() {
  $this->load->library('ValidationCode');
  $authcode = new ValidationCode();
   $authcode->outImage();
 }
 
复制代码

可以显示验证码
但改成以下形式的代码就提示:
A PHP Error was encounteredSeverity: Notice
Message: Undefined property: Admin:ValidationCode
Filename: controllers/admin.php
Line Number: 151(也就是$this->ValidationCode->outImage();这行就问题)
PHP复制代码
 
function authcode() {
  $this->load->library('ValidationCode');
  $this->ValidationCode->outImage();
 }
 
复制代码

这是怎么回事?

本版积分规则