驗證碼在WEB應用中非常重要,通常用來防止用戶惡意提交表單,如惡意注冊和登錄、論壇惡意灌水等。本文將通過實例講解使用PHP生成常見的驗證碼
先給看下 大致的效果
那么接下來的就直接貼代碼吧
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <?php $image = imagecreatetruecolor(100, 30); //創建畫布 $imagecolor = imagecolorallocate($image, 255, 255, 255); //背景色 imagefill($image, 0, 0, $imagecolor); //填充背景色 for($i=0;$i<4;$i++ ){ //循環4位數 $fontsize = 6; $fontcolor = imagecolorallocate($image, rand(0, 200), rand(0, 200), rand(0, 200)); $fontcontent = rand(0, 9); $x = $i*100/4 + rand(5, 15); $y = rand(5, 10); imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } for($i=0;$i<200;$i++ ){ //循環 添加干擾點 $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200)); $x = rand(1, 99); $y = rand(1, 29); imagesetpixel($image, $x, $y, $pointcolor); } for($i=0;$i<3;$i++){ //循環 添加干擾線 $linecolor = imagecolorallocate($image, rand(100, 250), rand(100, 250), rand(100, 250)); $x1 = rand(1, 25); $x2 = rand(50, 75); $y1 = rand(1, 15); $y2 = rand(15, 25); imageline($image, $x1, $y1, $x2, $y2, $linecolor); } header("content-type:image/png"); imagepng($image); imagedestroy($image); ?>再給大家分享一個可以生成中文驗證碼
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 <?php //1.qi啟用gd庫GD庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者生成圖片。 // 在網站上GD庫通常用來生成縮略圖或者用來對圖片加水印或者對網站數據生成報表。 session_start(); // 把GBK編碼的字符串轉換成UTF-8字符串,第一個參數之所以寫GBK,是因為本php文件在主機中存儲的編碼是GBK編碼 // UTF-8編碼新聞熱點
疑難解答