简单php验证码类
来源:互联网 发布:linux 拷贝进度 编辑:程序博客网 时间:2024/06/05 02:40
PHP 验证码类
今天做了一个验证码。遇到了两个坑爹的事情。一个是 header 前面不能有任何输出,哪怕是空格都不行。不然后面的输出都没有。其二是生成的验证码编码类型必须是 UTF-8无BOM格式。哪怕是utf-8无格式都不行。根据原理,二十分钟就写好了代码,可是就是无法输出图像。对比了无数文件,才算搞定。太坑爹了。。。TAT
下面总结下实现原理。
1.提供一个字符串作为随机因子。然后根据验证码长度,循环字符串,取出作为验证码值。
2.生成画布,将验证码的高度和宽带,长度等在构造函数初始化,以便传入参数灵活定制。
3.随机码配合字体,写入画布中。
4.讲随机线条,雪花生成,写入画布。5.输出图像6.构造一个对外调用接口
效果图
代码如下
//index.php
主文件
<?php/* Report all errors except E_NOTICE */error_reporting(E_ALL ^ E_NOTICE);//设置utf-8编码header('Content-Type:text/html;charset=utf-8');//设置硬路径define('ROOT_PATH',dirname(__FILE__));//引入 验证码类,当然还可以改写为自动加载require ROOT_PATH.'/ValidateCode.class.php';$_vc = new ValidateCode(4,130,50,20);$_vc->doimg();?>
//ValidateCode.class.php
类文件
<?phpclass ValidateCode{private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //随机因子private $code;//随机码private $codelen;//验证码位数private $img;//画布,资源句柄private $width;//验证码宽度private $height;//验证码高度private $font;//字体private $fontsize;//字体大小private $fontcolor;//字体颜色//构造函数,初始化参数,传入 验证码位数 宽度 高度 字体大小public function __construct($_codelen,$_width,$_height,$_fontsize){$this->codelen = $_codelen;$this->width = $_width;$this->height = $_height;$this->fontsize = $_fontsize;$this->font = ROOT_PATH.'/font/elephant.ttf';}//生成随机码private function createCode(){$_len = strlen($this->charset) - 1;for ($i=0; $i<$this->codelen; $i++) { $this->code .= $this->charset[mt_rand(0,$_len)]; }}//生成背景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);}//生成文字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]);}}//生成随机线条private function createLine(){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);}}//生成随机雪花private function createSnow(){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);}}//输出图像private function outPut(){header('Content-type:image/png');imagepng($this->img);imagedestroy($this->img);}//对外接口public function doimg(){$this->createBg();$this->createCode();$this->createLine();$this->createSnow();$this->createFont();$this->outPut();}}?>
代码分析:下载地址
- 简单php验证码类
- PHP-简单验证码类
- PHP验证码类,简单安全的PHP验证码
- PHP验证码类,简单安全的PHP验证码
- PHP验证码类,简单安全的PHP验证码
- php简单验证码
- php简单验证码
- 简单实用的PHP验证码类
- 【PHP】一个简单的验证码类
- php实现简单验证码并且验证
- php实现简单验证码
- php制作简单验证码
- php实现简单验证码
- php实现简单验证码
- PHP实现简单验证码
- php简单发送验证码
- php简单数字验证码
- 【php】php验证码的简单实现
- arm开发板6410移植boa服务器
- Web入门者必看的HTML代码编写的30条军规
- 等差数列
- 【转】spring MVC 转向(重定向)
- 等比数列
- 简单php验证码类
- 大型机汇编(mainframe assembler/HLASM)之代码loop篇
- PropertyEditorSupport
- wince程序窗体 居中显示[C#]
- boost::ref
- 点量源动闪媒Flash全景展示系统(可提供源码),提供360度全景展示
- 头文件 卫哨
- Properly encrypting with AES with CommonCrypto
- 参数文件