php语言

php实现验证码制作

时间:2024-08-12 00:32:01 php语言 我要投稿
  • 相关推荐

php实现验证码制作

  验证码分为:数字验证码,字母验证码,数字加字母验证码,图片验证码,汉子验证码,视频验证码等!由于原理相同,且根据平时的使用范围来看,今天在这里只讲数字验证码,字母验证码,数字加字母验证码。下面是由百分网小编为大家整理的php实现验证码制作,喜欢的可以收藏一下!了解更多详情资讯,请关注应届毕业生考试网!

  首先,看一张图了解验证码生成的过程。

  (1)生成验证码底图

  (2)验证码内容

  (3)生成验证码

  (4)对比校验

  验证码实现的核心技术分析

  (a)底图的实现,并添加干扰元素

  (b)生成验证内容

  (c)验证内容保存在服务端

  (d)验证内容的校验

  下面看代码实现的过程

  <?php

  /*

  这段代码 实现了产生 随机数字,随机数字+字母验证码

  */

  session_start();

  $image = imagecreatetruecolor(100, 30);

  //imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。

  $bgcolor = imagecolorallocate($image, 255, 255, 255);

  // imagecolorallocate — 为一幅图像分配颜色

  imagefill($image,0,0,$bgcolor);

  /*   生成字母验证码

  for($i=0;$i<4;$i++)

  {

  $fontsize = 6;

  // $fontcolor = imagecolorallocate($image, 0, 0, 0);

  $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));

  $fontcontent =rand(0,9);

  $x = ($i*100/4) + rand(5,10);

  $y = rand(5,10);

  // imagestring — 水平地画一行字符串

  imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);

  }

  */

  // 生成字母加数字的随机验证码

  $captcha_code = "";

  for($i=0;$i<4;++$i)

  {

  $fontsize = 6;

  $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));

  $data = "abcdefghijklmnopqrstuvwxtz123456789";

  $fontcontent = substr($data,rand(0,strlen($data)),1);

  $captcha_code.=$fontcontent;

  $x = ($i*100/4) + rand(5,10);

  $y = rand(5,10);

  imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);

  }

  $_SESSION['authcode']=$captcha_code;

  //  给验证码添加点干扰项

  for($i=0;$i<200;$i++)

  {

  $pointcolor = imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200));

  imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);

  // bool imagesetpixel ( resource $image , int $x , int $y , int $color );

  // imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。

  }

  // 增加线的干扰

  for($i=0;$i<3;++$i)

  {

  $linecolor = imagecolorallocate($image, rand(80,220), 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);

  ?>

【php实现验证码制作】相关文章:

php如何实现验证码06-13

PHP实现简易blog的制作10-27

如何实现php登陆表单提交CSRF及验证码09-14

一个漂亮的php验证码类10-04

仿照TP框架自带的PHP验证码类10-22

用php生成带有雪花背景的验证码09-30

PHP页面跳转实现技巧09-19

PHP中多态如何实现09-04

PHP实现抽奖概率算法09-13

PHP列表页实现的方法05-24