• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    PHP通过GD库实现验证码功能示例

    本文实例讲述了PHP通过GD库实现验证码功能。分享给大家供大家参考,具体如下:

    首先看看实现的效果:

    具体实现:

    ?php
    /*PHP实现验证码*/
    session_start();//开启会话
    //创建画布
    $image=imagecreatetruecolor(100,38);
    //背景颜色
    $bgcolor=imagecolorallocate($image,255,255,255);
    imagefill($image,0,0,$bgcolor);
    $captch_code='';//存储验证码
    //随机选取4个数字
    for($i=0;$i4;$i++){
      $fontsize=10;    //
      $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));//随机颜色
      $fontcontent=rand(0,9);
      $captch_code.=$fontcontent;
      $x=($i*100/4)+rand(5,10);  //随机坐标
      $y=rand(5,10);
      imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
    }
    /*//字母和数字混合验证码
    for($i=0;$i4;$i++) {
      $fontsize = 10;    //
      $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));//??????
      $data = 'abcdefghijklmnopqrstuvwxyz1234567890';  //数据字典
      $fontcontent = substr($data, rand(0, strlen($data)), 1);
      $captch_code.=$fontcontent;
      $x = ($i * 100 / 4) + rand(5, 10);
      $y = rand(5, 10);
      imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
    }*/
    $_SESSION['code']=$captch_code;
    //增加干扰点
    for($i=0;$i200;$i++){
      $pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
      imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);//
    }
    //增加干扰线
    for($i=0;$i3;$i++){
      $linecolor=imagecolorallocate($image,rand(80,280),rand(80,220),rand(80,220));
      imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
    }
    //输出格式
    header('content-type:image.png');
    imagepng($image);
    //销毁图片
    imagedestroy($image);
    
    

    实现表单的连接和“换一个”功能的实现:

    input type="text" placeholder="验证码" name="verifycode" class="captcha">br>br>
    img id="captcha_img" src="captcha.php?r=?php echo rand();?>" alt="验证码">
    label>a href="javascript:void(0)" rel="external nofollow" onclick="document.getElementById('captcha_img').src='captcha.php?r='+Math.random()">换一个/a> /label>
    
    

    captcha.php后面加上一个随机参数是为了实现换一个验证码的功能,,换一个功能通过简单的js即可实现。如果没有这个功能可以不用参数。

    更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

    希望本文所述对大家PHP程序设计有所帮助。

    您可能感兴趣的文章:
    • PHP5中GD库生成图形验证码(有汉字)
    • php中使用GD库做验证码
    • php通过GD库实现验证码功能
    • PHP使用GD库制作验证码的方法(点击验证码或看不清会刷新验证码)
    • PHP 用session与gd库实现简单验证码生成与验证的类方法
    • php结合GD库简单实现验证码的示例代码
    • php结合GD库实现中文验证码的简单方法
    上一篇:php中file_get_contents()函数用法实例
    下一篇:Laravel核心解读之异常处理的实践过程
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    PHP通过GD库实现验证码功能示例 PHP,通过,库,实现,验证,码,