ITPub博客

首页 > IT职业 > IT职场 > PHP验证码

PHP验证码

IT职场 作者:kikiv 时间:2021-09-09 09:18:13 0 删除 编辑

   session_start();

    //生成验证码图片

    Header("Content-type: image/PNG");

    $im = imagecreate(44,18); // 画一张指定宽高的图片

    $back = ImageColorAllocate($im, 245,245,245); // 定义背景颜色

    imagefill($im,0,0,$back); //把背景颜色填充到刚刚画出来的图片中

    $vcodes = "";

    srand((double)microtime()*1000000);

    //生成4位数字

    for($i=0;$i

    $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); // 生成随机颜色

    $authnum=rand(1,9);

    $vcodes.=$authnum;

    imagestring($im, 5, 2+$i*10, 1, $authnum, $font);

    }

    $_SESSION['VCODE'] = $vcodes;

 

    for($i=0;$i

    {

    $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));

    imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); // 画像素点函数

    }

    ImagePNG($im);

    ImageDestroy($im);

?>

将上面程序命名为verifycode.php,在需要调用验证码的位置添加如下代码:图片描述

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3585/viewspace-2801951/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 基础 PHP 语法
请登录后发表评论 登录
全部评论

注册时间:2019-08-14

  • 博文量
    68
  • 访问量
    822