脚本宝典收集整理的这篇文章主要介绍了既简单又安全的PHP验证码 附调用方法,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
protected static $_image = null; // 验证码图片实例
protected static $_color = null; // 验证码字体颜色
/**
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);
// 写<a href="https://www.js-code.COM/tag/yige/" target="_blank" class="keywords">一个</a>验证码字符
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");
// <a href="https://www.js-code.com/tag/shuchu/" target="_blank" class="keywords">输出</a>图像
imagepng(self::$_image);
imagedestroy(self::$_image);
}
/**
/
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::$imageH1.5,self::$imageL2); // 周期
$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<a href="https://www.js-code.com/tag/xingneng/" target="_blank" class="keywords">性能</a>要好很多
$i--;
}
}
}
$A = mt_rand(1,self::$imageH/2); // 振幅
$f = mt_rand(-self::$imageH/4,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,self::$_color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)<a href="https://www.js-code.com/tag/xingneng/" target="_blank" class="keywords">性能</a>要好很多
$i--;
}
}
}
}
/**
/**
}
// 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';
}
/
以上是脚本宝典为你收集整理的既简单又安全的PHP验证码 附调用方法全部内容,希望文章能够帮你解决既简单又安全的PHP验证码 附调用方法所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。