php生成验证码与验证码验证完整实例

第一种:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE  html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta  http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script  language="javascript">
 function refresh_code()
 {
  form1.imgcode.src="verifycode.php?a="+Math.random();
 }
</script>
</head>
<body>
<form  id="form1" name="form1" method="post" action="checkcode.php">
  <label  for="code">验证码:</label>
  <input  type="text" name="code" id="textfield" />
  <img  id="imgcode" src="VerifyCode.php" alt="验证码" />
  <a  href="javascript:refresh_code()">看不清?换一个</a>
  <input  type="submit" name="button" id="button" value="提交" />
</form>
</body>
</html>

  

  

verifycode.php文件代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
 /*
  图片验证码 Powered By KASON test <a href=" http://www.hzhuti.com/nokia/c6/"> http://www.hzhuti.com/nokia/c6/</a>   */
  session_start();
  $num=4;//验证码个数
  $width=80;//验证码宽度
  $height=20;//验证码高度
  $code=' ';
  for($i=0;$i<$num;$i++)//生成验证码
  {
   switch(rand(0,2))
   {
    case  0:$code[$i]=chr(rand(48,57));break;//数字
    case  1:$code[$i]=chr(rand(65,90));break;//大写字母
    case  2:$code[$i]=chr(rand(97,122));break;//小写字母
   }
  }
  $_SESSION["VerifyCode"]=$code;
  $image=imagecreate($width,$height);
  imagecolorallocate($image,255,255,255);
  for($i=0;$i<80;$i++)//生成干扰像素
  {
   $dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
   imagesetpixel($image,rand(1,$width),rand(1,$height),$dis_color);
  }
  for($i=0;$i<$num;$i++)//打印字符到图像
  {
   $char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
   imagechar($image,60,($width/$num)*$i,rand(0,5),$code[$i],$char_color);
  }
  header("Content-type:image/png");
  imagepng($image);//输出图像到浏览器
  imagedestroy($image);//释放资源
?> 

  checkcode.php文件如下

1
2
3
4
5
6
7
8
9
10
11
<meta http-equiv="Content-Type"  content="text/html; charset=utf-8"  />
<?php
ini_set('display_errors','Off');
session_start();
  if((strtoupper($_POST["code"])) ==strtoupper(($_SESSION["VerifyCode"]))){
 print("验证码正确,");
  }else{
    print("验证码错误,");
  }
  echo  "提交的验证码:".strtoupper($_POST["code"]).",正确的验证码:".strtoupper($_SESSION["VerifyCode"]);
?>

  

第二种:

form.php

1
2
3
4
5
6
<?php
session_start();  
    if(isset($_REQUEST['authcode'])){ 
        // 将存在session里面的验证码全部小写化
        $data_code = strtolower($_SESSION['authcode']);
    }  ?>

ver.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php 
// <span style="white-space:pre">  </span>//因为要把产生的验证码保存到session中,此处为session开始 
    session_start(); 
    //创建一张宽100高30的图像 
    $image = imagecreatetruecolor(100, 30); 
    //为$image设置背景颜色为白色 
    $bgcolor = imagecolorallocate($image, 255, 255, 255); 
    //填充背景颜色 
    imagefill($image, 0, 0, $bgcolor); 
   
    //生成4个随机数 
/*  for($i=0; $i<4; $i++){
        //设置字体为6
        $fontsize=6;
        //设置背景颜色为随机颜色 三个rand()函数分别对应颜色的rgb让他们产生在0~120这个范围的数值
        $fontcolor=imagecolorallocate($image, rand(0,120), rand(0, 120), rand(0,120));
        //生成随机数字
        $fontcontent=rand(0, 9);
        //控制数字出现的位置x->left y->top
        $x=($i*100/4)+rand(5, 10);
        $y=rand(5, 10);
  
        imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
  
    }
*/ 
    $captch_code=""
    for($i=0; $i<4; $i++){ 
        $fontsize=50; 
        $fontcolor=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0, 120)); 
        $data="1234567890abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ"
        //设置每次产生的字符从$data中每次截取一个字符 
        $fontcontent=substr($data, rand(0,strlen($data)), 1); 
        //让产生的四个字符拼接起来 
        $captch_code.=$fontcontent
        //控制每次出现的字符的坐标防止相互覆盖即x->left y->top 
        $x=($i*100/4)+rand(5, 10); 
        $y=rand(5, 10); 
        //此函数用来将产生的字符在背景图上画出来 
        imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); 
    
    $_SESSION['authcode']=$captch_code;//把产生的验证码存入session中 
    //用来在背景图片上产生200个干扰点 
    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); 
    
   
    //产生三条干扰线 
    for ($i=0; $i <3 ; $i++) {  
        # code... 
        //干扰线的颜色 
        $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图片格式为png 
    header('content-type:image/png'); 
    //显示图片 
    imagepng($image); 
   
   
    //destory 
    imagedestroy($image); 
   
?>

  

 
posted @   mingruqi  阅读(2534)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2017-01-13 Java集合
点击右上角即可分享
微信分享提示