找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1267|回复: 0
打印 上一主题 下一主题

既简单又安全的PHP验证码 附调用方法

[复制链接]

2500

主题

2513

帖子

7520

积分

论坛元老

Rank: 8Rank: 8

积分
7520
跳转到指定楼层
楼主
发表于 2018-2-14 05:35:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            一、验证码示例



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

* @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 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 0) {      
          imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多   
          $i--;
        }
      }
    }
  }
   
  /**
   * 画杂点
   * 往图片上写不同颜色的字母或数字
   */
  protected static function _writeNoise() {
    for($i = 0; $i  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
entry();  
?>  
2、检查验证码是否正确

entry();  
  $code = $_GET['code'];  
  echo $vcode->check($code);     
  //$_SESSION['code'] = $vc->getCode();//验证码保存到SESSION中
?>  
3、验证码输入框调用页面

单击图片重新获取验证码




以上就是本文的全部内容,希望对大家学习PHP程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • PHP中文汉字验证码
  • php图片验证码代码
  • php中文字母数字验证码实现代码
  • php5 图片验证码实现代码
  • PHP验证码类代码( 最新修改,完全定制化! )
  • 一个漂亮的php验证码类(分享)
  • ThinkPHP验证码使用简明教程
  • PHP生成图片验证码、点击切换实例
  • PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
  • 完美解决thinkphp验证码出错无法显示的方法
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端