php實現的數字驗證碼及數字運算驗證碼_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php實現的數字驗證碼及數字運算驗證碼。分享給大家供大家參考。具體如下:

1. 數字驗證碼:

<?php//第一個執行個體是數字驗證碼,最常見的驗證碼。多少個數字可以由自己決定。//$num是產生的驗證碼包含幾個數字getValidate(4,60,20);function getValidate($num,$w,$h){  $code = "";  for($i=0;$i<$num;$i++){    $code .= rand(0,9);  }  //code變數最後是$num個數字,並且是字串。因襲如果是產生四位的數字驗證碼,可以用rand(1000,9999)產生  Header("Content-type:image/PNG");  $img = imagecreate($w,$h);//建立圖片,長寬參數是一開始定義好的。  $black = imagecolorallocate($img,0,0,0);//定義黑色  $gray = imagecolorallocate($img,200,200,200);//定義灰色  $bgcolor = imagecolorallocate($img,255,255,255);//背景色白色  imagefill($img,0,0,$gray);  //imagefill($image,ing $x,int $y,int $color),在image映像的座標x,y處用color顏色執列區域填充  //給驗證碼畫上邊框,感覺驗證碼美美的,其實呢畫邊框就是給他畫上一個矩形  imagerectangle($img,0,0,$w-1,$h-1,$black);//用黑色的框框  /*imagestring($image,$font,$x,$y,$s,$col)  用col顏色將字串s畫到image所代表的映像的x,y處(xy是字串的左上方座標),  整幅映像的左上方為(0,0)如果font是1,2,3,4,5則使用內建字型*/  //一下是畫四個數位方法,讓四個字元位置都隨機等  $strx = rand(5,10);  for($i = 0;$i < $num;$i++){    $strops = rand(1,6);    imagestring($img,5,$strx,$strops,substr($code,$i,1),$black);    $strx += rand(8,12);    //strx是橫座標,需要每一次加上之前的座標,保證不會重疊。縱座標不管  }  //substr() 函數返回字串的一部分,substr(string,start,length),要截取的字串,start開始位置,length截取的長度  //產生好了數字,下面要給驗證碼地區一些幹擾,防止一些工具可以自動識別  //1.方法一:給背景加上很多噪點  //imagesetpixel($image,$x,$y,$color),在image映像中用color顏色在x,y座標(映像左上方為0,0)上畫一個點  for($i = 0;$i<80;$i++){    imagesetpixel($img, rand(0,$w), rand(0,$h), $black);//這些點分布在這個背景裡面  }  //2.方法二:繪製幾條虛線  $style = array($black,$gray);  imagesetstyle($img, $style);  //imagesetstyle($image,$style),設定畫線的風格,像素組成的數組  $y1 = rand(0,$h);  $y2 = rand(0,$h);  $y3 = rand(0,$h);  $y4 = rand(0,$h);  imageline($img,0,$y1,$w,$y2,IMG_COLOR_STYLED);  imageline($img,0,$y3,$w,$y4,IMG_COLOR_STYLED);  imagepng($img);  imagedestroy($img);}?>

2. 數字運算驗證碼:

<?phpgetValidate(100,30);function getValidate($w,$h){  $img = imagecreate($w,$h);  $gray = imagecolorallocate($img,255,255,255);  $black = imagecolorallocate($img,rand(0,200),rand(0,200),rand(0,200));  $red = imagecolorallocate($img, 255, 0, 0);  $white = imagecolorallocate($img, 255, 255, 255);  $green = imagecolorallocate($img, 0, 255, 0);  $blue = imagecolorallocate($img, 0, 0, 255);  imagefilledrectangle($img, 0, 0, 100, 30, $black);  for($i = 0;$i < 80;$i++){    imagesetpixel($img, rand(0,$w), rand(0,$h), $gray);  }  $num1 = rand(1,99);  $num2 = rand(1,99);  imagestring($img, 5, 5, rand(1,10), $num1, $red);  imagestring($img,5,30,rand(1,10),getRand(), $white);  imagestring($img,5,45,rand(1,10),$num2, $green);  imagestring($img,5,65,rand(1,10),"=", $blue);  imagestring($img,5,80,rand(1,10),"?", $red);  header("content-type:image/png");  imagepng($img);  imagedestroy($img);}function getRand(){  $code = rand(0,1);  switch ($code) {    case 0:      return "+";      break;    case 1:      return "-";      break;    default:      # code...      break;  }}?>

希望本文所述對大家的php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.