PHP七种不同的个性创意验证码

来源:https://www.sucaihuo.com/php/721.html 2016-03-22 07:35浏览(4260) 收藏

本demo的验证码共7种,验证方法就不写了,原理就是看用户输入的验证码和当前的session code进行对比,若是一致,说明正确,否则失败。具体PHP验证码验证方法,请看http://www.sucaihuo.com/jquery/5/533/demo/
PHP七种不同的个性创意验证码
分类:表单代码 > 验证码 难易:初级
查看演示 下载资源 下载积分: 20 积分

验证码图片html代码

<img src="1.php" onclick="this.src = '1.php?t=' + Math.random()" title="点击刷新" />

第一个图像验证码类,其他6种验证码类请下载查看

class captcha{
    /**
    +----------------------------------------------------------
    * 生成验证码
    +----------------------------------------------------------
    * @static
    * @access public
    +----------------------------------------------------------
    * @param int $len  验证码字符数
    * @param int $font_size  验证码字体大小
    * @param string $name  session名称
    * @param int $width  图片长度
    * @param int $height  图片高度
      +----------------------------------------------------------
    * @return void
      +----------------------------------------------------------
    */
    static function generate($len=4,$font_size=48,$name='captcha',$width='',$height=''){
        if($width=='') $width=($font_size+5)*($len+1);
        if($height=='') $height=($font_size)*2;
        $chars='bcdefhkmnrstuvwxyABCDEFGHKMNPRSTUVWXY345689';
        $str='';
        for($i=0;$i<$len;$i++){
            $str .= substr($chars,mt_rand(0,strlen($chars)-1),1);
        }
        $_SESSION[$name]=$str;//写入session
        for($num=0;$num<10;$num++){
            ob_start();
            $image=imagecreatetruecolor($width,$height);//创建图片
            $bg_color=imagecolorallocate($image,255,255,255);//设置背景颜色
            $border_color=imagecolorallocate($image,100,100,100);//设置边框颜色
            $text_color=imagecolorallocate($image,0,0,0);//设置验证码颜色
            imagefilledrectangle($image,0,0,$width-1,$height-1,$bg_color);//填充图片背景色
            imagerectangle($image,0,0,$width-1,$height-1,$border_color);//填充图片边框颜色
            for($i=0;$i<5;$i++){
                $line_color=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));//干扰线颜色
                imageline($image,rand(0,$width),0,$width,$height,$line_color);//画一条线段
            }
            for($i=0;$i<500;$i++){
                $dot_color=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));//干扰点颜色
                imagesetpixel($image,rand()%$width,rand()%$height,$dot_color);//画一个像素点
            }
            for($i=0;$i<$len;$i++){
                imagettftext($image,$font_size,rand(-3,3),$font_size/2+($font_size+5)*$i,$height/1.25-rand(2,3),$text_color,'Groupsex.ttf',$str[$i]);//用规定字体向图像写入文本
            }
            imagegif($image);
            imagedestroy($image);
            $imagedata[] = ob_get_contents();
            ob_clean();
        }
        require('GIFEncoder.class.php');
        $gif = new GIFEncoder($imagedata);
        ob_clean();//防止出现'图像因其本身有错无法显示'的问题
        header('Content-type:image/gif');
        echo $gif->GetAnimation();
    }
}
//调用示例
session_start();
captcha::generate(6,48);
标签: 验证码code
评论0
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791

1 2