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

首頁 > 開發 > PHP > 正文

PHP實現簡單漢字驗證碼

2024-05-04 23:38:08
字體:
來源:轉載
供稿:網友

大家知道簡單數字或者字母驗證碼很容易被破解,但是算式驗證碼或者中文漢字驗證碼不容易被破解,所以建議大家在使用驗證碼的時候,盡量用算式驗證碼或者中文漢字驗證碼。

現在越來越多的網站都開始使用漢字驗證碼了,既增加了我們國人的親切感,同時也增加了機器破解的難度,這里我就簡單粗暴的說一下。。。

創建背景畫布

 

 
  1. $image = imagecreatetruecolor(200, 60); 
  2. $background = imagecolorallocate($image, 255, 255, 255); 
  3. imagefill($image, 0, 0, $background); 

畫干擾點

 

 
  1. for ($i=0; $i < 300; $i++) {  
  2. $pixColor = imagecolorallocate($image, rand(150, 240), rand(150, 240), rand(150, 240)); 
  3. $pixX = rand(10, 190); 
  4. $pixY = rand(5, 55); 
  5. imagesetpixel($image, $pixX, $pixY, $pixColor); 

畫干擾線

 

 
  1. //4條水平線 
  2. for ($i=0; $i < 5; $i++) {  
  3. $lineColor = imagecolorallocate($image, rand(50, 150), rand(50, 150), rand(50, 150)); 
  4. $lineX1 = 0; 
  5. $lineX2 = 300; 
  6. $lineY1 = ($i + 1) * 12; 
  7. $lineY2 = ($i + 1) * 12; 
  8. imageline($image, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor); 
  9.  
  10. //10條垂直線 
  11. for ($i=0; $i < 30; $i++) {  
  12. $lineColor = imagecolorallocate($image, rand(50, 150), rand(50, 150), rand(50, 150)); 
  13. $lineX1 = ($i + 1) * 10; 
  14. $lineX2 = ($i + 1) * 10; 
  15. $lineY1 = 0; 
  16. $lineY2 = 60; 
  17. imageline($image, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor); 

畫漢字

 

 
  1. $text = array('梔''子''花''開'); 
  2. for ($i=0; $i < 4; $i++) { 
  3. $textColor = imagecolorallocate($image, rand(20, 100), rand(20, 100), rand(20, 100)); 
  4. $textX = $i * 50 + 10; 
  5. $textY = rand(40, 60); 
  6. imagettftext($image, 30, rand(20, 50), $textX, $textY, $textColor, "/Library/Fonts/華文仿宋.ttf", $text[$i]); 

這里注意一下,字體文件一定要支持中文的

編碼要使用utf-8,gbk的中文記得要轉嗎【iconv函數可以幫助你】

輸出圖像

 

 
  1. header("Content-Type:image/png"); 
  2. imagepng($image); 

銷毀資源

 

 
  1. imagedestroy($image); 

經過粗略的搞吧搞吧,中文驗證碼也就顯示出來了,當然一般網站使用的時候會有一個漢字庫種子,從里面隨機取出特定個數的漢字顯示,最后就是記錄到session進行驗證了。

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万荣县| 武威市| 木兰县| 无极县| 时尚| 青冈县| 将乐县| 耿马| 留坝县| 濉溪县| 浏阳市| 茌平县| 长汀县| 开封市| 临沭县| 上饶市| 张掖市| 项城市| 安义县| 齐齐哈尔市| 安溪县| 阿合奇县| 云林县| 腾冲县| 顺昌县| 永胜县| 北京市| 象山县| 微山县| 达孜县| 虞城县| 景东| 鄄城县| 山西省| 永昌县| 武邑县| 十堰市| 辽阳县| 平潭县| 池州市| 汤原县|