欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一个简单安全的PHP验证码类、PHP验证码

程序员文章站 2024-02-29 17:09:40
一,验证码示例 二,php验证码类,secoder.class.php

一,验证码示例

一个简单安全的PHP验证码类、PHP验证码

二,php验证码类,secoder.class.php

<?php 
/** 
* 安全验证码 
* 
* 安全的验证码要:验证码文字扭曲、旋转,使用不同字体,添加干扰码 
* 
* @author 流水孟春 <cmpan(at)qq.com> 
* @link http://labs.yulans.cn/yl_security_secoder 
* @link http://wiki.yulans.cn/docs/yl/security/secoder 
*/ 
class yl_security_secoder { 
/** 
* 验证码的session的下标 
* 
* @var string 
*/ 
//public static $sekey = 'sid.sek ey.ylans.cn'; 
public static $sekey = 'sid'; 
public static $expire = 3000; // 验证码过期时间(s) 
/** 
* 验证码中使用的字符,01io容易混淆,建议不用 
* 
* @var string 
*/ 
public static $codeset = '346789abcdefghjklmnpqrtuvwxy'; 
public static $fontsize = 25; // 验证码字体大小(px) 
public static $usecurve = true; // 是否画混淆曲线 
public static $usenoise = true; // 是否添加杂点 
public static $imageh = 0; // 验证码图片宽 
public static $imagel = 0; // 验证码图片长 
public static $length = 4; // 验证码位数 
public static $bg = array(243, 251, 254); // 背景 
protected static $_image = null; // 验证码图片实例 
protected static $_color = null; // 验证码字体颜色 
/** 
* 输出验证码并把验证码的值保存的session中 
* 验证码保存到session的格式为: $_session[self::$sekey] = array('code' => '验证码值', 'time' => '验证码创建时间'); 
*/ 
public static function entry() { 
// 图片宽(px) 
self::$imagel || self::$imagel = self::$length * self::$fontsize * 1.5 + self::$fontsize*1.5; 
// 图片高(px) 
self::$imageh || self::$imageh = self::$fontsize * 2; 
// 建立一幅 self::$imagel x self::$imageh 的图像 
self::$_image = imagecreate(self::$imagel, self::$imageh); 
// 设置背景 
imagecolorallocate(self::$_image, self::$bg[0], self::$bg[1], self::$bg[2]); 
// 验证码字体随机颜色 
self::$_color = imagecolorallocate(self::$_image, mt_rand(1,120), mt_rand(1,120), mt_rand(1,120)); 
// 验证码使用随机字体 
//$ttf = dirname(__file__) . '/ttfs/' . mt_rand(1, 20) . '.ttf'; 4 
$ttf = dirname(__file__) . '/ttfs/4.ttf'; 
if (self::$usenoise) { 
// 绘杂点 
self::_writenoise(); 
} 
if (self::$usecurve) { 
// 绘干扰线 
self::_writecurve(); 
} 
// 绘验证码 
$code = array(); // 验证码 
$codenx = 0; // 验证码第n个字符的左边距 
for ($i = 0; $i<self::$length; $i++) { 
$code[$i] = self::$codeset[mt_rand(0, 27)]; 
$codenx += mt_rand(self::$fontsize*1.2, self::$fontsize*1.6); 
// 写一个验证码字符 
imagettftext(self::$_image, self::$fontsize, mt_rand(-40, 70), $codenx, self::$fontsize*1.5, self::$_color, $ttf, $code[$i]); 
} 
// 保存验证码 
isset($_session) || session_start(); 
$_session[self::$sekey]['code'] = join('', $code); // 把校验码保存到session 
$_session[self::$sekey]['time'] = time(); // 验证码创建时间 
header('cache-control: private, max-age=0, no-store, no-cache, must-revalidate'); 
header('cache-control: post-check=0, pre-check=0', false); 
header('pragma: no-cache'); 
header("content-type: image/png"); 
// 输出图像 
imagepng(self::$_image); 
imagedestroy(self::$_image); 
} 
/** 
* 画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数) 
* 
* 高中的数学公式咋都忘了涅,写出来 
* 正弦型函数解析式:y=asin(ωx+φ)+b 
* 各常数值对函数图像的影响: 
* a:决定峰值(即纵向拉伸压缩的倍数) 
* b:表示波形在y轴的位置关系或纵向移动距离(上加下减) 
* φ:决定波形与x轴位置关系或横向移动距离(左加右减) 
* ω:决定周期(最小正周期t=2π/∣ω∣) 
* 
*/ 
protected static function _writecurve() { 
$a = mt_rand(1, self::$imageh/2); // 振幅 
$b = mt_rand(-self::$imageh/4, self::$imageh/4); // y轴方向偏移量 
$f = mt_rand(-self::$imageh/4, self::$imageh/4); // x轴方向偏移量 
$t = mt_rand(self::$imageh*1.5, self::$imagel*2); // 周期 
$w = (2* m_pi)/$t; 
$px1 = 0; // 曲线横坐标起始位置 
$px2 = mt_rand(self::$imagel/2, self::$imagel * 0.667); // 曲线横坐标结束位置 
for ($px=$px1; $px<=$px2; $px=$px+ 0.9) { 
if ($w!=0) { 
$py = $a * sin($w*$px + $f)+ $b + self::$imageh/2; // y = asin(ωx+φ) + b 
$i = (int) ((self::$fontsize - 6)/4); 
while ($i > 0) { 
imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 这里画像素点比imagettftext和imagestring性能要好很多 
$i--; 
} 
} 
} 
$a = mt_rand(1, self::$imageh/2); // 振幅 
$f = mt_rand(-self::$imageh/4, self::$imageh/4); // x轴方向偏移量 
$t = mt_rand(self::$imageh*1.5, self::$imagel*2); // 周期 
$w = (2* m_pi)/$t; 
$b = $py - $a * sin($w*$px + $f) - self::$imageh/2; 
$px1 = $px2; 
$px2 = self::$imagel; 
for ($px=$px1; $px<=$px2; $px=$px+ 0.9) { 
if ($w!=0) { 
$py = $a * sin($w*$px + $f)+ $b + self::$imageh/2; // y = asin(ωx+φ) + b 
$i = (int) ((self::$fontsize - 8)/4); 
while ($i > 0) { 
imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多 
$i--; 
} 
} 
} 
} 
/** 
* 画杂点 
* 往图片上写不同颜色的字母或数字 
*/ 
protected static function _writenoise() { 
for($i = 0; $i < 10; $i++){ 
//杂点颜色 
$noisecolor = imagecolorallocate( 
self::$_image, 
mt_rand(150,225), 
mt_rand(150,225), 
mt_rand(150,225) 
); 
for($j = 0; $j < 5; $j++) { 
// 绘杂点 
imagestring( 
self::$_image, 
5, 
mt_rand(-10, self::$imagel), 
mt_rand(-10, self::$imageh), 
self::$codeset[mt_rand(0, 27)], // 杂点文本为随机的字母或数字 
$noisecolor 
); 
} 
} 
} 
/** 
* 验证验证码是否正确 
* 
* @param string $code 用户验证码 
* @param bool 用户验证码是否正确 
*/ 
public static function check($code) { 
isset($_session) || session_start(); 
// 验证码不能为空 
if(empty($code) || empty($_session[self::$sekey])) { 
//echo $_session[self::$sekey]['code'].'1'; 
return false; 
} 
// session 过期 
if(time() - $_session[self::$sekey]['time'] > self::$expire) { 
unset($_session[self::$sekey]); 
//echo $_session[self::$sekey]['code'].'2'; 
return false; 
//return 0; 
} 
// if($code == $_session[self::$sekey]['code']) { 
if(strtoupper($code) == $_session[self::$sekey]['code']) { //不区分大小写比较 
//echo $_session[self::$sekey]['code'].'3'; 
return true; 
} 
//echo $_session[self::$sekey]['code'].'4'; 
return false; 
} 
} 
// useage 
/* 
yl_security_secoder::$usenoise = false; // 要更安全的话改成true 
yl_security_secoder::$usecurve = true; 
yl_security_secoder::entry(); 
*/ 
/* 
// 验证验证码 
if (!yl_security_secoder::check(@$_post['secode'])) { 
print 'error secode'; 
} 
*/

三,调用方法

1,显示验证码页面code.php

<?php 
session_start(); 
require 'secoder.class.php'; //先把类包含进来,实际路径根据实际情况进行修改。 
$vcode = new yl_security_secoder(); //实例化一个对象 
$vcode->entry(); 
?>

2,检查验证码是否正确

<?php 
session_start(); 
require 'secoder.class.php'; //先把类包含进来,实际路径根据实际情况进行修改。 
$vcode = new yl_security_secoder(); //实例化一个对象 
//$vcode->entry(); 
$code = $_get['code']; 
echo $vcode->check($code); 
//$_session['code'] = $vc->getcode();//验证码保存到session中 
?>

3,验证码输入框调用页面

<img id="messageimg" src='images/tishis2.gif' width='16' height='16'> 单击图片重新获取验证码<br> 
<a href="#"><img src="code.php" onclick="javascript:this.src='code.php?tm='+math.random();" />

以上所述是小编给大家介绍的一个简单安全的php验证码类、php验证码,希望对大家有所帮助