欢迎光临
感谢一路有你

phpGD库-验证码

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 

英文+数字验证码

需要用到的函数
substr

$str = '1234567890qwertyuiopasdfghjklzxcvbnmZXCVBNMASDFGHJKLQWERTYUIOP';
        switch ($type) {
            case 1 :
                //表示验证码是纯数字
                $start = 0;
                $end = 9;
                break;
            case 2 :
                //表示验证码是纯字母
                $start = 10;
                $end = 61;
                break;
            case 3 :
                //数字和字母
                $start = 0;
                $end = 61;
                break;
        }
        //生成随机的字符串
        $newstr = '';
        for($i = 0; $i < $length; $i++){
            $sj = mt_rand($start,$end);
            $newstr .= substr($str, $sj, 1);//***********
        }

中文验证码

需要用到的函数
mb_substr
mb_strlen

//echo mb_substr($str ,0,4,'utf-8');截取4个
//echo mb_strlen($str,'utf-8');// 获取长度x,,原始3x
$str = '们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自肩腾枯抛轨堂拌爸循诱祝励肯酒绳穷塘燥泡袋朗喂铝软渠颗惯贸粪综墙趋彼届墨碍启逆卸航衣孙龄岭骗休借';
$newstr = '';
for($i = 0; $i < 4; $i++){
    $start = mt_rand(0,mb_strlen($str,'utf-8')-1);//每次减去1个
    $newstr .= mb_substr($str,$start,1,'utf-8');
}

echo $newstr;

案例

<?php
    header("content-type:text/html;charset=utf-8");
    date_default_timezone_set('PRC');
/**
 * @param int $width 验证码宽度
 * @param int $height 验证码高度
 * @param int $type 验证码类型
 * @param int $length 验证码长度
 */
function vcode($width=100,$height=40,$type=1,$length=4)
{
    //建立资源画布
    $img = imagecreatetruecolor($width,$height);
    //颜色
    $white = imagecolorallocate($img,250,250,250);
    $gray = imagecolorallocate($img,200,200,200);
    $black = imagecolorallocate($img,0,0,0);
    //填充背景
    imagefill($img,0,0,$gray);
    //作画
    //画点
    for($a=0;$a<$width;$a++){
        imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$black);
    }
    //画线
    for($a=0;$a<$height/4;$a++){
imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$black);
    }
    //文本
    $str = '1234567890qwertyuiopasdfghjklzxcvbnmZXCVBNMASDFGHJKLQWERTYUIOP';
    //选择验证码的方式是否为数字/字母/数字和字母
    switch($type){
        case 1:
            $start =0 ;
            $end = 9;
                break;
        case 2:
            $start =10;
            $end = 61;
            break;
        case 3:
            $start =0 ;
            $end = 61;
            break;
    }
    //随机数
    $newstr = '';
    for ($a=0;$a<$length;$a++){
        $sj = mt_rand($start,$end);
        $newstr .=substr($str,$sj,1);
    }
    for($a=0;$a<$length;$a++){
        $x = floor($width / $length) * $a + 5;
        $y = mt_rand(ceil($height /2) ,$height);
        imagettftext($img,25,0,$x,$y,$black,'./font/3.ttf',$newstr[$a]);
    }

    //判断验证码
    session_start();
    $_SESSION['code'] = $newstr;

    //输出验证码
    header("content-type:image/jpeg");
    imagejpeg($img);
    //销毁
    imagedestroy($img);
}

vcode();
vcode(500,100,3,5);

赞(2) 打赏
未经允许不得转载:王明昌博客 » phpGD库-验证码
分享到: 更多 (0)

相关推荐

  • 暂无文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

×
订阅图标按钮