提高 php 验证码 安全,简单安全的PHP验证码

一,验证码示例

4078afdcd239a28c69057fa9e0326a23.png

二,php验证码类,secoder.class.php'验证码值', '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; $i0) {

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';

}

*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值