关于thinkphp下验证码的问题

时间:2022-08-17 06:26:20

在今天做后台的时候遇到的一个问题,郁闷了一下午。现在搞好了就简单总结一个。

首先验证码是基于GD库,也就是说我们必须现开启php中的GD库

要使用验证码,需要导入扩展类库中的ORG.Util.Image类库和ORG.Util.String类库。我们通过在在模块类中增加一个verify方法来用于显示验证码:

  1. Public function verify(){

  2.    import('ORG.Util.Image');

  3.    Image::buildImageVerify();

ok,我们直接在模版中调用这个方法

定义完成后,验证码的显示只需要在模板文件中添加:

  1. <img    src='/Public/verify/' />

    就到这里,不乏显示,为什么,官方的说法是:UTF8文件中的BOM头在作怪。ok

网上有人做了一个清除BOM的php文件,我们只需要访问一下就行代码如下

<?php
/*清除rom*/
if(isset($_GET['dir'])){
$basedir=$_GET['dir'];
}else{
$basedir = '.';
}
$auto = 1;
checkdir($basedir);
function checkdir($basedir){
if($dh = opendir($basedir)){
while(($file = readdir($dh)) !== false){
if($file != '.' && $file != '..'){
if(!is_dir($basedir."/".$file)){
echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>";
}else{
$dirname = $basedir."/".$file;
checkdir($dirname);
}
}
}//end while
closedir($dh);
}//end if($dh
}//end function
function checkBOM($filename){
global $auto;
$contents = file_get_contents($filename);
$charset[1] = substr($contents, 0, 1);
$charset[2] = substr($contents, 1, 1);
$charset[3] = substr($contents, 2, 1);
if(ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191){
if($auto == 1){
$rest = substr($contents, 3);
rewrite ($filename, $rest);
return "<font color=red>BOM found, automatically removed.</font>";
}else{
return ("<font color=red>BOM found.</font>");
}
}
else return ("BOM Not Found.");
}//end function
function rewrite($filename, $data){
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>


一下是我运行的结果

关于thinkphp下验证码的问题

还有记录一个好看一些的验证码类

hinkphp集成了验证码类,可是样式需要修改 默认的太丑了。这时候我们需要找到 ORG/Util/Image类

在image你可以看到处理图片的方法都在这里面了。找到280多行的buildImageVerify方法  把原本的给注视掉 加上如下代码即可在这之前需要一个字体 位置放在根下的 Public/font/font.ttf没有字体使用默认的验证码字符

如下代码就是 验证码类修改版。

static function buildImageVerify($length=4, $mode=1, $type='png', $width=48, $height=22, $size='20',$verifyName='verify') {
      $fontpath=  dirname(__PATH__).'/Public/font/font.ttf';
      import('ORG.Util.String');//引入字符串类
      $randval = String::rand_string($length, $mode);//随机获得 $length个数字
     $_SESSION[$verifyName] = md5($randval);//把验证码存入session
       $width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;//判断如果验证码的宽度小于宽度字符串*10+10
      if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
          $im = imagecreatetruecolor($width, $height);
      } else {
          $im = imagecreate($width, $height);
      }
      $r = Array(225, 255, 255, 223);
      $g = Array(225, 236, 237, 255);
      $b = Array(225, 236, 166, 125);
      $key = mt_rand(0, 3);

      //$backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);    //背景色(随机)

      $backColor = imagecolorallocate($im, 17, 168, 171);                         //自定义背景颜色
      $borderColor = imagecolorallocate($im, 100, 100, 100);                    //验证码边框色

      imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);  //画一个矩形
      imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
      $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));//设定随机颜色
      $whites=imagecolorallocate($im,255,255,255);//设定随机颜色
      // 干扰
      for ($i = 0; $i < 2; $i++) {
          imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);//增加线条干扰
      }
      for ($i = 0; $i < 1; $i++) {
          imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);//增加像素干扰

      }

      $y = $height - ($height - $size) / 2;
       if(file_exists($fontpath)){
            for ($i = 0; $i < $length; $i++) {
                $x = $size * $i + $left+10;
                imagettftext($im, $size, mt_rand(10, 10), $x, $y, $whites, $fontpath, $randval{$i});
              }
        }else{
            for ($i = 0; $i < $length; $i++) {
             imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $whites);

           }
     }
      Image::output($im, $type);
  }
后台调用的时候设置字体参数即可修改字体大小
 import('ORG.Util.Image');

Image::buildImageVerify($length=4, $mode=1, $type='png', $width=100, $height=30,$size=30);