• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php写验证码类

php 搞代码 3年前 (2022-01-23) 13次浏览 已收录 0个评论

简介:这是php写验证码类的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

class=’pingjiaF’ frameborder=’0′ src=’http://biancheng.dnbcw.info/pingjia.php?id=341730′ scrolling=’no’>

前一段时间我写了个验证码函数, 今天做成了验证码类 有助于面向对象编程。

img.php

 1 <?php 2 /** 3 * QQ:279861795 4 * Author: gwyy 5 * Date: 2011-7-01 6 *通用验证码类 7 *版本:V0.1 8 */ 9 10 class ValidateCode {11    <strong>(本文来源gaodai#ma#com搞@@代~&码网</strong><pre>搞代gaodaima码

private $charset=”abcdefghizklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890″; //随机因子 12 private $code; //验证码文字13 private $codelen=4; //验证码显示几个文字14 private $width=130; //验证码宽度15 private $height=50; //验证码高度16 private $img; //验证码资源句柄17 private $font; //指定的字体18 private $fontsize=20; //指定的字体大小19 private $fontcolor; //字体颜色 随机20 21 //构造类 编写字体22 public function __construct(){23 $this->font=ROOT_PATH.’/font/elephant.ttf’;24 }25 //创建4个随机码26 private function createCode(){27 $_leng=strlen($this->charset);28 for($i=1;$icodelen;$i++){29 $this->code.=$this->charset[mt_rand(0,$_leng)];30 }31 return $this->code;32 }33 34 //创建背景35 private function createBg(){36 //创建画布 给一个资源jubing37 $this->img=imagecreatetruecolor($this->width,$this->height);38 //背景颜色39 $color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));40 //画出一个矩形41 imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);42 }43 44 //创建字体45 private function createFont(){46 $_x=($this->width / $this->codelen); //字体长度47 for ($i=0;$icodelen;$i++){48 //文字颜色49 $color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));50 //资源句柄 字体大小 倾斜度 字体长度 字体高度 字体颜色 字体 具体文本51 imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$color,$this->font,$this->code[$i]);52 }53 }54 //随机线条55 private function createLine(){56 //随机线条57 for ($i=0;$iimg,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));59 imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);60 }61 //随机雪花62 for ($i=0;$iimg,mt_rand(220,255),mt_rand(220,255),mt_rand(220,255));64 imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),’*’,$color);65 }66 }67 //输出背景68 private function outPut(){69 //生成标头70 header(‘ContentType:img/png’);71 //输出图片72 imagepng($this->img);73 //销毁结果集74 imagedestroy($this->img);75 }76 //对外输出77 public function doimg(){78 //加载背景79 $this->createBg();80 //加载文件81 $this->createCode();82 //加载线条83 $this->createLine();84 //加载字体85 $this->createFont();86 //加载背景87 $this->outPut();88 }89 90 //获取验证码91 public function getCode(){92 return strtolower($this->code);93 }94 95 }96 97 ?>

其他页面调用方法

index.php

<?php

require ‘img.php’;

$img=new ValidateCode();

echo $img->doimg();

?>

过往云烟 2011/07/29

爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

http://biancheng.dnbcw.info/php/341730.html pageNo:6


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php写验证码类

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址