思路
1.用php生成驗證碼文件 xx.php 用圖片輸出,同時在session 存儲這個str
2.在登入界面 src引入這個圖片 [ img src="vcode.php" onclick="this.src='vcode.php?id='+Math.random()" ] 點擊刷新 增加參數隨機數
3.判斷界面 check 轉小寫 刪除空格 通過session判斷驗證碼正確性[ $ncode=strtolower(str_replace(' ','',$code));]
<?php // 1.創建畫布資源 $img=imagecreatetruecolor(150,50); // 2.準備顏色 $black=imagecolorallocate($img,0,0,0); $white=imagecolorallocate($img,255,255,255); $red=imagecolorallocate($img,255,0,0); $green=imagecolorallocate($img,0,255,0); $blue=imagecolorallocate($img,0,0,255); $gray=imagecolorallocate($img,180,180,180); // 3.填充畫布 imagefill($img,0,0,$black); $arr=array_merge(range(0,9),range(a,z),range(A,Z)); shuffle($arr); $str=join(' ',array_slice($arr,0,4)); //畫字 imagettftext($img,20,0,20,35,$white,'ms.ttf',$str); //干擾素 for($i=0;$i<30;$i++){ imagearc($img,mt_rand(0,150),mt_rand(0,50),mt_rand(0,150),mt_rand(0,50),mt_rand(0,360),mt_rand(0,360),$white); } // 5.輸出最終圖像或保存最終圖像 header('content-type:image/png'); // 圖片從瀏覽器上輸出 imagepng($img); // 把圖片保存到本地 // imagejpeg($img,'jin.jpg'); // 6.釋放畫布資源 imagedestroy($img); ?>