php验证码类
来源:互联网 发布:js的eval方法 编辑:程序博客网 时间:2024/05/18 15:07
1、Verify.class.php
/** * 验证码类 * @author mark * @since 2013-10-15 */class Verify{ private $width; private $height; private $codeNum; private $code; private $im; function __construct($width=80, $height=30, $codeNum=4) { $this->width = $width; $this->height = $height; $this->codeNum = $codeNum; } function showImg() { //创建图片 $this->createImg(); //设置干扰元素 $this->setDisturb(); //设置验证码 $this->setCaptcha(); //输出图片 $this->outputImg(); }//获取验证码 function getCode() { return strtolower($this->code); } private function createImg() { $this->im = imagecreatetruecolor($this->width, $this->height); $bgColor = imagecolorallocate($this->im, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255)); imagefill($this->im, 0, 0, $bgColor); } private function setDisturb() { $area = ($this->width * $this->height) / 20; $disturbNum = ($area > 250) ? 250 : $area; //加入点干扰 for ($i = 0; $i < $disturbNum; $i++) { $color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color); } //加入弧线 for ($i = 0; $i <= 5; $i++) { $color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255)); imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color); } } private function createCode() { $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ"; for ($i = 0; $i < $this->codeNum; $i++) { $this->code .= $str{rand(0, strlen($str) - 1)}; } } private function setCaptcha() { $this->createCode(); for ($i = 0; $i < $this->codeNum; $i++) { $color = imagecolorallocate($this->im, mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); $size = rand(floor($this->height / 5), floor($this->height / 3)); $x = floor($this->width / $this->codeNum) * $i + 5; $y = rand(0, $this->height - 20); imagechar ($this->im, $size, $x, $y, $this->code{$i}, $color); } } private function outputImg() { if (imagetypes() & IMG_JPG) { header('Content-type:image/jpeg'); imagejpeg($this->im); } elseif (imagetypes() & IMG_GIF) { header('Content-type: image/gif'); imagegif($this->im); } elseif (imagetype() & IMG_PNG) { header('Content-type: image/png'); imagepng($this->im); } else { die("Don't support image type!"); } imagedestroy($this->im); }}
2、verify.php
require_once 'salesbangV1/util/Verify.class.php';$verify = new Verify();$verify->showImg();$_SESSION["verifyCode"] = $verify->getCode();//验证码保存到SESSION中
0 0
- php验证码类
- php验证码类
- PHP验证码类
- php验证码类
- php 验证码类
- php 验证码类
- PHP 验证码类
- PHP验证码类
- php验证码类
- php验证码类
- php验证码类
- php验证码类
- php验证码类
- php验证码类
- php验证码类
- php验证码类
- PHP验证码类,简单安全的PHP验证码
- PHP验证码类,简单安全的PHP验证码
- ajax json 传值注意
- source命令与点命令(.)
- [Leetcode] Two Sum (Java)
- DBCC大全集之(适用版本MS SQLServer 2008 R2)----DBCC PROCCACHE以表格格式显示有关过程缓存的信息
- cppquery:用C++模仿jquery的探索
- php验证码类
- Eclipse安装SVN插件
- Pomelo 新手入门(一)
- Unity 基础知识
- 关于SharedPreferences 保存list数据到持久层
- jq获取表单值与赋值代码
- STM32的SPI时钟
- Makefile
- xcode 如何设置第三方库不使用ARC