相思资源网 Design By www.200059.com

一、验证码示例

一个简单安全的PHP验证码类 附调用方法

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

<"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

<"color: #800000">2.检查验证码是否正确

<"color: #800000">3.验证码输入框调用页面

<img id="messageImg" src='images/tishis2.gif' width='16' height='16'> 单击图片重新获取验证码<br>
<a href="#"><img src="/UploadFiles/2021-04-02/code.php">

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
PHP,验证码

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“一个简单安全的PHP验证码类 附调用方法”

暂无一个简单安全的PHP验证码类 附调用方法的评论...