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

首頁 > 語言 > PHP > 正文

PHP驗證碼的產生和生成驗證碼程序

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

由于注冊的時候常常會用到注冊碼來防止機器惡意注冊,這里我發表一個產生png圖片驗證碼的基本圖像,很簡陋但思想很清晰:

1、產生一張png的圖片

2、為圖片設置背景色

3、設置字體顏色和樣式

4、產生4位數的隨機的驗證碼

5、把產生的每個字符調整旋轉角度和位置畫到png圖片上

6、加入噪點和干擾線防止注冊機器分析原圖片來惡意注冊

7、輸出圖片

8、釋放圖片所占內存

authcode.php文件,代碼如下:

  1. <?php 
  2.         session_start (); 
  3.         header ( 'content-type: image/png' ); 
  4.         //創建圖片 
  5.         $im = imagecreate($x=130,$y=45 ); 
  6.         $bg = imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155)); //第一次對 imagecolorallocate() 的調用會給基于調色板的圖像填充背景色 
  7.         $fontcolor = imagecolorallocate ( $im, 255, 255, 255 );   //字體顏色 
  8.         $fontstyle = 'rock.ttf';                   //字體樣式,這個可以從c:windows onts文件夾下找到,我把它放到和authcode.php文件同一個目錄,這里可以替換其他的字體樣式 
  9.         //產生隨機字符 
  10.         for($i = 0; $i < 4; $i ++) { 
  11.                 $randasciinumarray         = array (rand(48,57),rand(65,90)); 
  12.                 $randasciinum                 = $randasciinumarray [rand ( 0, 1 )]; 
  13.                 $randstr                         = chr ( $randasciinum ); 
  14.                 imagettftext($im,30,rand(0,20)-rand(0,25),5+$i*30,rand(30,35),$fontcolor,$fontstyle,$randstr); 
  15.                 $authcode                        .= $randstr;  
  16.         } 
  17.         $_session['authcode']        = $randfourstr;//用戶和用戶輸入的驗證碼做比較 
  18.         //干擾線 
  19.         for ($i=0;$i<8;$i++){ 
  20.                 $linecolor        = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); 
  21.                 imageline ($im,rand(0,$x),0,rand(0,$x),$y,$linecolor); 
  22.         }//開源代碼Vevb.com 
  23.         //干擾點 
  24.         for ($i=0;$i<250;$i++){ 
  25.                 imagesetpixel($im,rand(0,$x),rand(0,$y),$fontcolor); 
  26.         } 
  27.         imagepng($im); 
  28.         imagedestroy($im);                 
  29. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 靖州| 开化县| 临沭县| 肇源县| 聂拉木县| 清原| 汾阳市| 长汀县| 景宁| 阳城县| 钟山县| 潢川县| 夏河县| 凤凰县| 永仁县| 昌宁县| 赫章县| 凤冈县| 桦川县| 汤原县| 双鸭山市| 北海市| 龙门县| 德惠市| 普兰县| 安乡县| 旌德县| 泸州市| 漠河县| 全州县| 平南县| 广河县| 赣榆县| 普陀区| 五台县| 依安县| 连州市| 广德县| 彰武县| 黔江区| 屯留县|