php生成验证码需要注意的

发布时间:2020-08-02 19:21:06 作者:huangyanxiong
来源:网络 阅读:383

<?php
    session_start();
    $gd=gd_info();
    if (!$gd['FreeType Support']){
        exit('该服务不支持gdlib');
    }
    //文件头...
    header("Content-type: p_w_picpath/png");
    //创建真彩色白纸
    $im = @p_w_picpathcreatetruecolor(100, 40) or die("建立图像失败");
    //获取背景颜色
    $background_color = p_w_picpathcolorallocate($im, 250, 205, 205);
    //填充背景颜色(这个东西类似油桶)
    p_w_picpathfill($im,0,0,$background_color);
    //获取边框颜色
    $border_color = p_w_picpathcolorallocate($im,200,200,200);
    //画矩形,边框颜色200,200,200
    p_w_picpathrectangle($im,0,0,100,40,$border_color);
    //逐行炫耀背景,全屏用1或0
    for($i=2;$i<40;$i++){
        //获取随机淡色     
        $line_color = p_w_picpathcolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
        //画线
        p_w_picpathline($im,0,$i,100,$i,$line_color);
    }
    //设置字体大小
    $font_size=28;
    //设置印上去的文字
    $Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $Str[1] = "abcdefghijklmnopqrstuvwxyz";
    $Str[2] = "01234567891234567890123456";
    //获取第1个随机文字
    $imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)];
    $imstr[0]["x"] = rand(2,5);
    $imstr[0]["y"] = rand(1,4);
    //获取第2个随机文字
    $imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)];
    $imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1);
    $imstr[1]["y"] = rand(1,3);
    //获取第3个随机文字
    $imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)];
    $imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1);
    $imstr[2]["y"] = rand(1,4);
    //获取第4个随机文字
    $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];
    $imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1);
    $imstr[3]["y"] = rand(1,3);
    //写入随机字串
    for($i=0;$i<4;$i++){
        //获取随机较深颜色
        $text_color = p_w_picpathcolorallocate($im,rand(50,180),rand(50,180),rand(50,180));
        //画文字
        p_w_picpathstring($im,$font_size,$imstr[$i]["x"],rand(0, 20),$imstr[$i]["s"],$text_color);
        //p_w_picpathttftext($im, $font_size, 0 , $imstr[$i]["x"], rand(0, 20), $text_color, font.ttf, $imstr[$i]["s"]);
$captcha=$captcha.$imstr[$i]["s"];  //只要这里进行字符串连接时,验证码就不能显示
    }
    $_SESSION['captcha']=$captcha;
    //显示图片
    p_w_picpathpng($im);
    //销毁图片
   p_w_picpathdestroy($im);
?

1:生成图像的php页面不能有含有一些没有初始化的变量

原因:这些没有初始化的变量会出现一个notice提示,输出到图片流中,打乱图片流的代码,导致验证码不能显示,这就好比session_start()前不能有任何的输出,但是这个notice提示是很少人能够看得出来的,在这个例子中的$captcha就是没有初始化的,需要在使用这个变量前要初始化,就不会有问题了



推荐阅读:
  1. php注入记录需要注意什么
  2. php验证码生成类

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

验证码 字符串.php 需要注意

上一篇:Android 浏览器的研究(五)--- 浏览器APK的Eclipse开发环境搭建

下一篇:Azure AD User 同步 _ User Attrib

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》