php 自己写的验证码类--刚学PHP一个月 高手进来指教下
自己写的验证码类--刚学PHP一个月 高手进来指教下 1. 本人刚学PHP一个月,自己写了个验证码类,希望高手指教下。 2. 验证码配色为统一颜色,只是透明不同,感觉这样更安全些。 分享
- 本人刚学PHP一个月,自己写了个验证码类,希望高手指教下。 2. 验证码配色为统一颜色,只是透明不同,感觉这样更安全些。 分享&互助
class.Captcha.php ~ 5KB 下载(16)
<?php
/**
* @package 验证码类
* @version 1.2 <2012-03-01>
* @author 小楠 <xn@uinan.com>
* @link www.uinan.com
* @copyright UINAN.COM
*/
class Captcha {
/**
* Captcha()
* =========== 方法名(参数) ===========/=== 返回值 ===/============说明============/
* getImg() 图像资源 显示验证码
* checkText($String) Boolean 验证输入字符串是否与验证码相等
* true相等, false不等
* ===================================/=============/============================/
*/
private $_fontDir = '../font/ariali.ttf'; // 验证码字体
private $_imgWidth = 120; // 背景宽度
private $_imgHeight = 40; // 背景高度
private $_imgText = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; // 验证码字符集
private $_imgTextLen = 4; // 验证码长度
private $_imgTextSP = 24; // 验证码字符间距
private $_imgTextT = 18; // 验证码最大字体
private $_imgTextB = 14; // 验证码最小字体
private $_imgTextRot = true; // 字体倾斜 treu为启用 false为关闭
private $_imgBGLine = 5; // 背景干扰线数量
private $_imgLine = 3; // 横向干扰线数量
private $_imgDot = 100; // 干扰点数量
private $_img ; // 图像标识符(真彩图)主要用于析构函数进行垃圾回收
private function _getEncryptText($str)
{
$encrypt = md5(strtoupper($str));
return $encrypt;
}
function getImg()
{
session_start();
// 定义页面信息
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Pragma: no-cache");
header("Cache-control: private");
header('Content-Type: image/png');
// 新建真彩图
$this->_img = imagecreatetruecolor($this->_imgWidth, $this->_imgHeight);
// 重定向变量
$img = $this->_img;
$imgwidth = $this->_imgWidth;
$imgheight = $this->_imgHeight;
$imgbgline = $this->_imgBGLine;
$imgline = $this->_imgLine;
$imgdot = $this->_imgDot;
$imgtextlen = $this->_imgTextLen;
$imgtext = $this->_imgText;
$imgrot = $this->_imgTextRot;
$imgtextb = $this->_imgTextB;
$imgtextt = $this->_imgTextT;
$imgtextsp = $this->_imgTextSP;
$fontdir = $this->_fontDir;
// 真彩图背景色
$imgColor = imagecolorallocate($img, 255, 255, 255);
// 填充真彩图
imagefill($img, 0, 0, $imgColor);
// 随机RGB值
$r = rand(50, 120);
$g = rand(50, 120);
$b = rand(50, 120);
// 干扰线颜色
$linecolor = imagecolorallocatealpha($img, $r, $g, $b, 85);
// 背景干扰线
for ($i=0; $i < $imgbgline; $i++) {
imagesetthickness($img, rand(8, 16));
imageline($img, rand(5, 60), rand(0, $imgheight), rand($imgwidth-5, $imgheight-60), rand($imgheight, 0), $linecolor);
}
// 横向干扰线
for ($i=0; $i < $imgbgline; $i++) {
imagesetthickness($img, rand(4, 8));
imageline($img, 0, rand(0, $imgheight), $imgwidth, rand($imgheight, 0), $linecolor);
}
// 干扰点
for ($i=0; $i < $imgdot; $i++) {
$dotcolor = imagecolorallocatealpha($img, $r, $g, $b, rand(0,20));
imagesetpixel($img, rand(0, $imgwidth), rand(0,$imgheight), $dotcolor);
}
// 文字颜色
$imgTextColor = imagecolorallocate($img, $r, $g, $b);
//session数组
$sessionvalue = array();
// 文字X轴起点
$textx = rand(2,20);
// 根据验证码长度循环写入字符
for ($i=0; $i < $imgtextlen; $i++) {
// 取得验证码字符集的长度
$strlen = strlen($imgtext);
// 随机取一个验证码字符集内字符
$text = $imgtext[rand(0, $strlen-1)];
// 把取得字符添加到sessionvalue数组
$sessionvalue[$i] = $text;
if ( $imgrot !== false ) {
$textrot = rand(-20, 20);
}else{
$textrot = 0;
}
imagettftext($img, rand($imgtextb, $imgtextt), $textrot, $textx+($i*$imgtextsp), rand(20, $imgheight-4), $imgTextColor, $fontdir, $text);
}
// 把sessionvalue数组内元素合并为字符串
$captchaText = implode('', $sessionvalue);
// 把字符串保存到$_SESSION['Captcha']中
$_SESSION['captcha'] = $this->_getEncryptText($captchaText);
imagepng($img);
// 清除图像资源
imagedestroy($this->_img);
}
function checkText($str)
{
$ss_captcha = isset( $_SESSION['captcha'] ) ? trim( stripslashes( $_SESSION['captcha'] ) ) : '';
$str_ec = $this->_getEncryptText($str);
if ( $str_ec == $ss_captcha ) {
return true;
unset($_SESSION['captcha']);
}else{
return false;
}
}
}
?>
精彩图集
精彩文章






