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

首頁 > 語言 > PHP > 正文

php生成驗證碼函數

2024-09-04 11:42:29
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php生成驗證碼函數的相關資料,需要的朋友可以參考下,php生成驗證碼的函數,實用靠譜。先上下生成的驗證碼的效果圖(這里生成的是全數字的驗證碼的示例效果):

下面是php生成驗證碼的源碼:

  1. <?php  
  2. session_start();  
  3. //session_register('CheckCode');  
  4. //PHP4.2以上版本不需要用session_register()注冊SESSION變量  
  5. $type='gif';  
  6. $width= 45;  
  7. $height= 20;  
  8. header("Content-type: image/".$type);  
  9. srand((double)microtime()*1000000);  
  10. if(isset($_GET['action'])){  
  11.  $randval=randStr(4,$_GET['action']);   
  12. }else{  
  13.  $randval=randStr(4,'');  
  14. }  
  15. if($type!='gif'&&function_exists('imagecreatetruecolor')){  
  16.  $im=@imagecreatetruecolor($width,$height);  
  17. }else{  
  18.  $im=@imagecreate($width,$height);  
  19. }  
  20. $r=Array(225,211,255,223);  
  21. $g=Array(225,236,237,215);  
  22. $b=Array(225,236,166,125);  
  23. $key=rand(0,3);  
  24. $backColor=ImageColorAllocate($im,$r[$key],$g[$key],$b[$key]);//背景色(隨機)  
  25. $borderColor=ImageColorAllocate($im,127,157,185);//邊框色  
  26. $pointColor=ImageColorAllocate($im,255,170,255);//點顏色  
  27. @imagefilledrectangle($im,0,0,$width - 1,$height - 1,$backColor);//背景位置  
  28. @imagerectangle($im,0,0,$width-1,$height-1,$borderColor); //邊框位置  
  29. $stringColor=ImageColorAllocate($im,255,51,153);  
  30. for($i=0;$i<=100;$i++){  
  31.  $pointX=rand(2,$width-2);  
  32.  $pointY=rand(2,$height-2);  
  33.  @imagesetpixel($im,$pointX,$pointY,$pointColor);  
  34. }  
  35. @imagestring($im,5,5,1,$randval,$stringColor);  
  36. $ImageFun='Image'.$type;  
  37. $ImageFun($im);  
  38. @imagedestroy($im);  
  39. $_SESSION['CheckCode']=$randval;  
  40. function randStr($len=6,$format='ALL'){  
  41.  switch($format){  
  42.   case 'ALL'://生成包含數字和字母的驗證碼  
  43.    $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'break;  
  44.   case 'CHAR'://僅生成包含字母的驗證碼  
  45.    $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'break;  
  46.   case 'NUMBER'://僅生成包含數字的驗證碼  
  47.    $chars='0123456789'break;  
  48.   default :  
  49.    $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'break;  
  50.  }  
  51.  $string='';  
  52.  while(strlen($string)<$len)  
  53.  $string.=substr($chars,(mt_rand()%strlen($chars)),1);  
  54.  return $string;  

該函數的具體使用方法請看如下這個示例(這里是生成全數字的驗證碼):

<img src="checkCode.php?action=NUMBER" width="45" height="20" />

以上就是php如何生成驗證碼的實現函數,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武安市| 叙永县| 齐齐哈尔市| 吉木萨尔县| 德兴市| 西乡县| 泸溪县| 洪洞县| 邛崃市| 定南县| 靖宇县| 福鼎市| 沙雅县| 金溪县| 东乡县| 定西市| 紫阳县| 营山县| 克东县| 格尔木市| 八宿县| 兴化市| 民勤县| 中江县| 额敏县| 团风县| 承德市| 会东县| 沛县| 石城县| 荔波县| 寻甸| 四子王旗| 大足县| 海阳市| 宁河县| 湘潭市| 汽车| 延吉市| 古丈县| 清流县|