PHP比较好的验证码.doc_第1页
PHP比较好的验证码.doc_第2页
PHP比较好的验证码.doc_第3页
PHP比较好的验证码.doc_第4页
PHP比较好的验证码.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

?php/* Verification Code Class* Used to anti-spam base at PHP GD Lib* author Eric,* version 1.0* copyright Ereesoft Inc. 2009* update 2009-05-14 22:32:05* example* session_sratr();* $vcode = new Vcode();* $vcode-setLength(5);* $_SESSIONvcode = $vcode-paint();/ To be encrypted by MD5*/class Vcode /* * var $width The width of the image,auto Calculated 验证图片宽度,程序自动计算 */ private $width; /* * var $height Image height 验证图片高度 */ private $height; /* * var $length Verification Code lenght 验证码长度 */ private $length; /* * var $bgColor Image background color default random 验证图片背景色 */ private $bgColor; /* * var $fontColor The text color 验证码颜色 */ private $fontColor; /* * var $dotNoise The number of noise 噪点数量 */ private $dotNoise; /* * var $lineNoise The number of noise lines 干扰线数量 */ private $lineNoise; /* * var $im image resource GD图像操作资源 */ private $im; /* * void Vcode:Vcode() * * The constructor */ public function Vcode() $this-dotNoise = 20;/初始化噪点数量 $this-lineNoise = 2;/初始化干扰线数量 /* * void Vcode:setLength(integer $length) * * Set Verification Code length * access public * param integer $length; * return void */ public function setLength($length) $this-length = $length; /* * void Vcode:setBgColor(string $bgColor) * * Set background color of the Verification Image * access public * param string $bgColor e.g.: #ffffff;可以直接使用css书写中的16进制写法,但不可简写 * return void */ public function setBgColor($bgColor) $this-bgColor = sscanf($bgColor, #%2x%2x%2x); /* * void Vcode:setFontColor(string $fontgColor) * * Set text color of the Verification Image * access public * param string $fontColor e.g.: #ffffff;可以直接使用css书写中的16进制写法,但不可简写 * return void */ public function setFontColor($fontColor) $this-fontColor = sscanf($fontColor, #%2x%2x%2x); /* * void Vcode:setDotNoise(integer $num) * * How many noise dots want to draw * access public * param integer $num Too much will lower performance * return void */ public function setDotNoise($num) $this-dotNoise = $num;/手动设置噪点数量后,会覆盖初始设置 /* * void Vcode:setLineNoise(integer $num) * * How many noise lines want to draw * access public * param integer $num Too much will lower performance * return void */ public function setLineNoise($num) $this-lineNoise = $num;/手动设置干扰线数量后,会覆盖初始设置 /* * String Vcode:randString() * * Create Random characters 生成随机字符串 * access private * return String */ private function randString() $string = strtoupper(md5(microtime().mt_rand(0,9); return substr($string, 0, $this-length); /* * void Vcode:drawDot() * * Draw dots noise 根据制定的数量随机画噪点,噪点颜色也随机 * access private */ private function drawDot() for($i=0; $idotNoise; $i+) $color = imagecolorallocate($this-im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255);/生成随机颜色 imagesetpixel($this-im, mt_rand(0,$this-width), mt_rand(0,$this-height), $color);/在随机生成的坐标上画噪点 /* * void Vcode:drawLine() * * Draw line noise 随机颜色随机画干扰线 * access private */ private function drawLine() for($i=0; $ilineNoise; $i+) $color = imagecolorallocate($this-im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255);/随机生成颜色 imageline($this-im, mt_rand(0,$this-width), mt_rand(0,$this-height), mt_rand(0,$this-width), mt_rand(0,$this-height), $color);/在随机生成的坐标上画干扰线 /* * String Vcode:paint() * * Create image and output * access public * return string The Verification Code to be encrypted by MD5 */ public function paint() if(empty($this-length) $this-length = 4;/验证码默认长度为4 $this-width = $this-length*12+4 ;/计算验证图片宽度 $this-height = 20;/制定验证码图片高度 $this-im = imagecreate($this-width, $this-height);/创建画布 if(empty($this-bgColor) | empty($this-fontColor)/如果没有设置前景色和背景色则全部随机 /避免前景色和背景色过于接近,背景色(0-130)的随机范围与前景色(131-255)分开 imagecolorallocate( $this-im, mt_rand(0,130), mt_rand(0,130), mt_rand(0,130); $randString = $this-randString(); for($i=0; $ilength; $i+) $fontColor = imagecolorallocate($this-im, mt_rand(131,255), mt_rand(131,255), mt_rand(131,255); imagestring($this-im, 3, $i*10+8, mt_rand(0,8), $randString$i, $fontColor); /单个验证码字符高度随机,避免被OCR else /如果设置了背景色和前景色,则不使用随机颜色 imagecolorallocate( $this-im, $this-bgColor0, $this-bgColor1, $this-bgColor2); $randString = $this-randString(); $fontColor = imagecolorallocate($this-im, $this-fontColor0, $this-fontColor1, $this-fontColor2); for($i=0;$ilength;$i+) imagestring($this-im, 3, $i*10+8, mt_rand(0,8), $randString$i, $fontColor);/每个验证码字符高度仍然随机 $this-drawDot();/绘制噪点 $this-drawLine();/绘制干扰线 imagepng($this-im); imagedestroy($this-im); return md5($randString);/返回MD5加密后的验证码,可直接放入session 下面是调用方法session_sratr();$vcode = new Vcode();$vcode-setLength(5);$_SESSIONvcode = $vcode-paint();/ To be encrypted by MD5_首先我们建立一个login.html 代码如下: 用户名: 密码: 验证码:   由以上代码我们可以看出 我们必需建立两个PHP 文件 红色的PHP文件是我们要提交的目标文件也就验证提交信息是否合法的主要文件而蓝色的PHP文件则是我们的生成验证码的文件。那么我们现在首先来看看code.php 生成验证码的文件 代码如下:?phpsession_start();$_SESSIONre_code=;$type = gif;$width= 38;$height= 15;header(Content-type: image/.$type);srand(double)microtime()*1000000);$randval = randStr(4,ALL);if($type!=gif & function_exists(imagecreatetruecolor) $im = imagecreatetruecolor($width,$height);else $im = imagecreate($width,$height); $r = Array(225,211,255,223); $g = Array(225,236,237,215); $b = Array(225,236,166,125); $key = rand(0,3); $backColor = ImageColorAllocate($im,$r$key,$g$key,$b$key);/背景色(随机) $borderColor = ImageColorAllocate($im, 20, 66, 111);/边框色 $pointColor = ImageColorAllocate($im, 255, 170, 255);/点颜色 imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);/背景位置 imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor); /边框位置 $stringColor = ImageColorAllocate($im, 255,255,255); for($i=0;$i=100;$i+) $pointX = rand(2,$width-2); $pointY = rand(2,$height-2); imagesetpixel($im, $pointX, $pointY, $pointColor); imagestring($im, 3, 5, 1, $randval, $stringColor); $ImageFun=Image.$type; $ImageFun($im); ImageDestroy($im); $_SESSIONre_code = $randval;/产生随机字符串function randStr($len=6,$format=ALL) switch($format) case ALL: $chars=ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789; break; case CHAR: $chars=ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz; break; case NUMBER: $chars=123456789; break; default : $chars=ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789; break; $string=; while(strlen($string)以上代码 保存成PHP文件即可生成gif的验证码图片,在生成图片的同时 也将我们的生成的验证码保存到了 $_SESSIONre_code 这个全局变量里 所以我们在login.php 这个文件里 只需要判断用户填写的验证码是否与它相等 就可以了。(提示:在login.php的最上方一定要先写 session_start(); 才能使用 $_SESSIONre_code) 写的很粗糙希望对看到此文的人有所帮助!_代码一:?php/* Filename: authpage.php* Author: hutuworm* Date: 2003-04-28* Copyleft */srand(double)microtime()*1000000);/验证用户输入是否和验证码一致if(isset($HTTP_POST_VARSauthinput)if(strcmp($HTTP_POST_VARSauthnum,$HTTP_POST_VARSauthinput)=0)echo 验证成功!;elseecho 验证失败!;/生成新的四位整数验证码while($authnum=rand()10000)请输入验证码:input type=hidden name=authnum value=img src=authimg.php?authnum=代码二:?php/* Filename: authimg.php* Author: hutuworm* Date: 2003-04-28* Copyleft */生成验证码图片Header(Content-type: image/PNG);srand(double)microtime()*1000000);$im = imagecreate(58,28);$black = ImageColorAllocate($im, 0,0,0);$white = ImageColorAllocate($im, 255,255,255);$gray = ImageColorAllocate($im, 200,200,200);imagefill($im,68,30,$gray);/将四位整数验证码绘入图片imagestring($im, 5, 10, 8, $HTTP_GET_VARSauthnum, $black);for($i=0;$i本文程序在Apache 2.0.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论