用php显示验证码

 时间:2026-02-16 20:48:46

1、//一、获取生成图像的数据,如验证码的四位字符(如何保障数据随机性的处理)。$authnum=random(4);

2、//二、新建一个图像(注意生成图像格式)。Header("Content-type: image/PNG");$im = imagecreate(55,18); //imagecreate() 新建图像,大小为 x_size 和 y_size 的空白图像。

3、//三、生成填充颜色$red = ImageColorAllocate($im, 153,51,0); //设置背景颜色$white = ImageColorAllocate($im, 255,204,0);//设置文字颜色$gray = ImageColorAllocate($im, 102,102,0); //设置杂点颜色

4、//四、填充背景颜色imagefill($im,55,18,$red);

5、//五、填充数据字符for ($i = 0; $i < strlen($authnum); $i++){    imagestring($im, 6, 13*$i+4, 1, substr($authnum,$i,1), $white); }

6、//六、填充杂色for($i=0;$i<100;$i++) imagesetpixel($im, rand()%55 , rand()%18 , $gray); //加入干扰象素

7、//七、输出图像ImagePNG($im); //以 PNG 格式将图像输出到浏览器或文件ImageDestroy($im);//销毁一图像

8、//产生随机数函数function random($length) {    $hash = '';    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';    $max = strlen($chars) - 1;    mt_srand((double)microtime() * 1000000);    for($i = 0; $i < $length; $i++) {        $hash .= $chars[mt_rand(0, $max)];    }    return $hash;}

  • 怎么用PHP写一个验证码
  • ThinkPHP验证码配置
  • PHPvod如何设置验证码
  • 解决Tthinkphp验证码不显示的问题
  • phpstorm代码提示如何设置
  • 热门搜索
    湖南旅游景点 新加坡旅游攻略 郑州旅游 旅游电子商务 旅游景点推荐 土耳其旅游 南京旅游攻略 西安周边旅游景点 贵州旅游地图 四川旅游景点