1. 程式人生 > >php實現驗證碼與表單的結合使用

php實現驗證碼與表單的結合使用

表單 FormIdentifyingCode.php

<form action="AuthcodeidentifyCode.php" method="post">
            <img src="identifyingCode.php"  title="看不清楚,換一張"  onclick="this.src='identifyingCode.php?url='+Math.random()" align=absmiddle>
         <p> 請輸入驗證碼:<input type="text" name="code" > </p>
            <input type="submit" value="提交">
     </form>

驗證碼生成頁 identifyingCode.php

<?php

session_start();
header('content-type:text/html;charset=utf-8;');
error_reporting(E_ALL & ~E_NOTICE);  //表示提示除去 E_NOTICE 之外的所有錯誤資訊
check_code();
    function check_code($width=100,$height=50,$num=4,$type='jpeg'){

        $img =  imagecreate($width,$height);
         $string ='';

          for($i=0;$i<$num;$i++)
          {
              $rand = mt_rand(0,2);
              switch($rand)
              {
                  case 0:
                      $ascii = mt_rand(48,57); //0-9
                      break;
                  case 1:
                      $ascii = mt_rand(65,90); //A-Z
                      break;
                  case 2:
                      $ascii = mt_rand(97,122); //a-z
                      break;
              }
              $string  .= sprintf('%c',$ascii); //把4個字元累加起來

          }
        $_SESSION['authcode'] =  $string ;
            //背景顏色
            imagefilledrectangle($img,0,0,$width,$height,randBg($img));

           //畫干擾元素
           // 我們可以隨機的在圖片中畫上50個畫素點。最小的位置為0,0。最大的位置為最大的寬或者最大的高。
           //然後使用mt_rand(0,最大寬)、mt_rand(0,最大高)。再使用randPix針對我們建立的畫布來分配顏色。
              //bool imagesetpixel ( resource $image , int $x , int $y , int $color )   在指定的座標處繪製畫素。
            for($i=0;$i<50;$i++){
                imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),randPix($img));
            }

             //寫字
            //PHP imagechar - 寫出橫向字元
        //bool imagechar ( resource $image , int $font , int $x , int $y , string $c , int $color )
        //imagechar() 將字串 c 的第一個字元畫在 image 指定的影象中,其左上角位於 x,y(影象左上角為 0, 0),顏色為 color。如果 font 是 1,2,3,4 或 5,則使用內建的字型(更大的數字對應於更大的字型)
            for($i=0;$i<$num;$i++){
                $x = floor($width/$num)*$i ;
                $y =   mt_rand(0,$height-15);
                imagechar($img,5,$x,$y,$string[$i],randPix($img));
            }

        //imagejpeg
        $func = 'image' . $type;
        $header = 'Content-type:image/' . $type;
        if (function_exists($func)) {
            header($header);
            $func($img);
        } else {
            echo '圖片型別不支援';
        }
        imagedestroy($img);
        return $string;
    }



//淺色的背景
// 0-120 低數值是深色系。
//130 - 255 通常為淺色系。RGB
function randBg($img){
        return imagecolorallocate($img,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
}
//深色的字或者點這些干擾元素
    function randPix($img){
        return imagecolorallocate($img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
    }
?>

驗證表單頁面 FormIdentifyingCode.php

<?php
session_start();
//error_reporting(E_ALL & ~E_NOTICE);  //表示提示除去 E_NOTICE 之外的所有錯誤資訊
header("Content-Type:text/html;charset=utf-8");      //設定頭部資訊

  if(isset($_POST['code'])){
      echo $_SESSION['authcode']."<br>";
      echo $_POST['code']."<br>";
      if(strtolower($_POST['code']) == strtolower($_SESSION['authcode'])){
          echo "<script language=\"javascript\">location.href='welcome.php';</script>";

      }
      else{
          echo "驗證碼錯誤";
      }
  }
  else{
      echo "沒有資訊";
  }
?>

登陸成功頁面  welcome.php

<?php
    echo "歡迎登陸";
?>