zoukankan      html  css  js  c++  java
  • php生成验证码与验证码验证完整实例

    第一种:
    <!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文件代码如下

    <?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文件如下

    <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

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

    ver.php

    <?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);  
      
    ?> 
    

      

     
  • 相关阅读:
    一个很吊的swing循环生成窗口。
    hbase操作的问题
    hadoop+hbase
    linux故障救援
    管道命令xargs
    hadoop浅尝 hadoop与hbase交互
    linux源代码阅读笔记 free_page_tables()分析
    词法分析器flex的使用
    每天一个Linux命令(1): find
    梯度下降
  • 原文地址:https://www.cnblogs.com/Im-Victor/p/12190007.html
Copyright © 2011-2022 走看看