国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php入門級的PHP驗證碼程序

2024-05-04 21:57:19
字體:
來源:轉載
供稿:網(wǎng)友

生成驗證碼我們會要用到php 圖形處理函數(shù),如imagecreate,imagepng,header之類的函數(shù),下面我們一起來看個簡單的實例。

實例代碼如下:

  1. <?php   
  2. session_start();   
  3.    
  4. $im = imagecreate(80,30);//創(chuàng)建圖片   
  5. $color = imagecolorallocate($im,rand(150,200),rand(150,200),rand(150,200));//設置圖片背景   
  6. $str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";//產(chǎn)生隨機字符串   
  7.  for($i=0;$i<5;$i++){   
  8.         $code .= $str[rand(0,(strlen($str)-1))];   
  9.         }   
  10. $_SESSION['code'] = $code;   
  11. for($a=0;$a<5;$a++){  //將字符串寫入圖片資源   
  12.     $x = $a*10 + 15;   
  13.     $y = rand(5,10);  // www.111cn.net 
  14.     imagechar($im,5,$x,$y,$code{$a},imagecolorallocate($im,0,0,0));   
  15.     }   
  16. header("Content-type:image/png");//輸出圖片資源   
  17. imagepng($im);   
  18. ?> 

例2代碼如下:

  1. <?php 
  2. if(!isset($_SESSION)){ //判斷session是否開啟 
  3. session_start(); //開啟就session 
  4. }  
  5. $width=70; //布畫寬度 
  6. $height=25; //布畫高度 
  7. $length=4;//驗證碼長度 
  8. $code=getcode($length); //獲取隨機字符串 
  9. $_SESSION['verfyCode'] = $code
  10. $img=imagecreate($width,$height); 
  11. $bgcolor=imagecolorallocate($img,240,240,240); 
  12. $rectangelcolor=imagecolorallocate($img,150,150,150); 
  13. imagerectangle($img,1,1,$width-1,$height-1,$rectangelcolor);//畫邊框 
  14. for($i=0;$i<$length;$i++){//循環(huán)寫字 
  15. $codecolor=imagecolorallocate($img,mt_rand(50,200),mt_rand(50,128),mt_rand(50,200)); 
  16. $angle=rand(-20,20); 
  17. $charx=$i*15+8; 
  18. $chary=($height+14)/2+rand(-1,1); 
  19. imagettftext($img,15,$angle,$charx,$chary,$codecolor,'C:WINDOWSFontsSIMKAI.TTF'
  20. $code[$i]); 
  21. for($i=0;$i<20;$i++){//循環(huán)畫線 
  22. $linecolor=imagecolorallocate($img,mt_rand(0,250),mt_rand(0,250),mt_rand(0,250)); 
  23. $linex=mt_rand(1,$width-1); 
  24. $liney=mt_rand(1,$height-1); 
  25. imageline($img,$linex,$liney,$linex+mt_rand(0,4)-2,$liney+mt_rand(0,4)-2,$linecolor); 
  26. for($i=0;$i<100;$i++){//循環(huán)畫點 
  27. $pointcolor=imagecolorallocate($img,mt_rand(0,250),mt_rand(0,250),mt_rand(0,250)); 
  28. imagesetpixel($img,mt_rand(1,$width-1),mt_rand(1,$height-1),$pointcolor); 
  29. function getcode($length){//生成php隨機數(shù)  
  30. $pattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ';//字符池  
  31. for($i=0;$i<$length;$i++) {  
  32. $key .= $pattern{mt_rand(0,35)};  
  33. }  
  34. return $key
  35. ob_clean(); 
  36. header('Content-type:image/png');  
  37. imagepng($img); 
  38. ?> 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: SHOW| 聂荣县| 长阳| 视频| 五峰| 长海县| 开阳县| 永泰县| 鹤岗市| 广饶县| 关岭| 崇义县| 吐鲁番市| 京山县| 卓尼县| 双柏县| 平塘县| 荥阳市| 额尔古纳市| 博野县| 获嘉县| 靖宇县| 德令哈市| 阳朔县| 桑植县| 台北县| 淮安市| 博罗县| 萨迦县| 肥西县| 晋宁县| 临城县| 怀安县| 阿图什市| 梨树县| 宁波市| 怀来县| 兰溪市| 那曲县| 思茅市| 南投市|