PHP Verification Code Processing

Source: Internet
Author: User

//基本步骤 1,先创建一张指定宽度和高度的一张验证码图片 imagecreatetruecolor() 2,给验证码图片添加背景颜色 和文字颜色 imagecolorallocate() 3, 在指定图片上面,画一个矩形 imagefilledrectangle() 4, 获取随机数  (定义一个函数) get_rand_str()    1,定义一个字符串    2,将上面的字符串 打乱  str_shuffle ()    3,并且从这个打乱的字符串当中去截取一部分内容 substr ()    4,把截取出来的字符串转化成小写字母 strtolower ()    5, 将这个字符串 return 5,将随机数写入到这个图片里面去 imagestring() 6,防止别人去恶意刷我们的验证码 可以在这个图片上面加上一些点 imagesetpixel() 7,开启session会话  将我们的验证码 存储到session当中与我们表单当中输入的验证码进行匹配 8,输入图片的 头信息 和 图片资源 删除 header( "Content-Type:image/png" ); imagepng( $img ); imagedestroy( $img ); 9,在登录界面 获取表单输入的验证码  和 我们session当中的验证码进行对比 如果正确就跳转登录界面 否则重新输入     html:         "img_code.php" />   img_code.php      //处理随机字符函数         function get_rand_str($length = 4){          $chars = ‘123456789ABCDEFGHJKLMNPQRSTUVWXYZ‘;          $str = str_shuffle($chars); // 随机打乱一个字符串          $str = substr($str,0,$length);          $str = strtolower($str);          return $str;         }      $width = 45;   //缩略图宽度      $height = 18;  //缩略图高度      $img = imagecreatetruecolor($width,$height);  // 新建一个真彩色图像            //imagecolorallocate()  为一幅图像分配颜色      /*      *$img  为哪幅图分配颜色      *74 147 223 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制      */      $backgroundcolor = imagecolorallocate($img,74,147,223);  //背景颜色      $textcolor = imagecolorallocate($img,255,255,255);   //文字颜色      //imagefilledrectangle — 画一矩形并填充      /*      * $img  图片资源      * 0,0,$width,height  分别为矩形的坐标      * $backgroundcolor 表示填充的颜色      */      imagefilledrectangle($img,0,0,$width,height,$backgroundcolor);      $get_code = get_rand_str();  //获取随机数            /*      水平地画一行字符串      imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )      imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)      如果 font 是 1,2,3,4 或 5,则使用内置字体。      */      imagestring($img,5,6,1,$get_code,$textcolors);            //在图片当中去画一些点 防止有人而已破解验证码      //imagesetpixel — 画一个单一像素      /*      *imagesetpixel ( resource $image , int $x , int $y , int $color )      *imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。      */      for($i=0;$i<=20;$i++){        $x = mt_rand(0,$width);        $y = mt_rand(0,$height);        imagesetpixel($img,$x,$y,imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));      }      session_start();  //开启session会话控制将验证码缓存      $_SESSION[‘imgcode‘] = $get_code//把生成的随机数放到session里面的一个变量当中      header("Content-Type:image/png");      imagepng($img);   //在浏览器上面输出一张图片      imagedestroy($img);  //销毁 图片

PHP Verification Code Processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.