• 技术文章 >php教程 >php手册

    实用PHP验证码类代码(1/2)

    2016-06-13 11:25:10原创403
    开发应用中验证码是少不了的,我们经常会碰以关于被机器注册,那么有了验证码可以有效的防止这类行为,下面我们来看看我提供的这款代码。

    开发应用中验证码是少不了的,我们经常会碰以关于被机器注册,那么有了验证码可以有效的防止这类行为,下面我们来看看我提供的这款代码。

    session_start();
    class authnum {
    //图片对象、宽度、高度、验证码长度
    private $im;
    private $im_width;
    private $im_height;
    private $len;
    //随机字符串、y轴坐标值、随机颜色
    private $randnum;
    private $y;
    private $randcolor;
    //背景色的红绿蓝,默认是浅灰色
    public $red=238;
    public $green=238;
    public $blue=238;
    /**
    * 可选设置:验证码类型、干扰点、干扰线、y轴随机
    * 设为 false 表示不启用
    **/
    //默认是大小写数字混合型,1 2 3 分别表示 小写、大写、数字型
    public $ext_num_type='';
    public $ext_pixel = false; //干扰点
    public $ext_line = false; //干扰线
    public $ext_rand_y= true; //y轴随机
    function __construct ($len=4,$im_width='',$im_height=25) {
    // 验证码长度、图片宽度、高度是实例化类时必需的数据
    $this->len = $len; $im_width = $len * 15;
    $this->im_width = $im_width;
    $this->im_height= $im_height;
    $this->im = imagecreate($im_width,$im_height);
    }
    // 设置图片背景颜色,默认是浅灰色背景
    function set_bgcolor () {
    imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
    }
    // 获得任意位数的随机码
    function get_randnum () {
    $an1 = 'abcdefghijklmnopqrstuvwxyz';
    $an2 = 'abcdefghijklmnopqrstuvwxyz';
    $an3 = '0123456789';
    if ($this->ext_num_type == '') $str = $an1.$an2.$an3;
    if ($this->ext_num_type == 1) $str = $an1;
    if ($this->ext_num_type == 2) $str = $an2;
    if ($this->ext_num_type == 3) $str = $an3;
    for ($i = 0; $i < $this->len; $i++) {
    $start = rand(1,strlen($str) - 1);
    $randnum .= substr($str,$start,1);
    }
    $this->randnum = $randnum;
    $_session[an] = $this->randnum;
    }
    // 获得验证码图片y轴
    function get_y () {
    if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);
    else $this->y = $this->im_height / 4 ;
    }

    1 2

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:php Memcached分布式缓存(1/3) 下一篇:简单php在线编辑保存php文件实现代码
    Web大前端开发直播班

    相关文章推荐

    • IIS php环境配置PHP5 MySQL5 ZendOptimizer phpmyadmin安装与配置• PHP获取网卡地址的代码• php.ini 中文版• PHP安全编程之加密功能• PHP与EXCEL PHPExcel

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网