html代碼:
複製代碼 代碼如下:
簡單驗證碼
JS代碼:
複製代碼 代碼如下:
function chkyzm(form){ //對驗證碼進行驗證
if(form.yzm.value==""){
yzm1.innerHTML="×驗證碼不可為空";
}else if(form.yzm.value!=form.yzm2.value){
yzm1.innerHTML="×驗證碼輸入錯誤";
}else{
yzm1.innerHTML="√驗證碼輸入正確";
}
}
function yzm(){ //產生驗證碼
var num1=Math.round(Math.random()*1000000);//隨機小數放大
var num=num1.toString().substr(0,4);//取4位整數
var yzm2=document.getElementById("yzm2");
document.write("");
yzm2.value=num;
}
function code(){ //重設驗證碼
var num1=Math.round(Math.random()*1000000);
var num=num1.toString().substr(0,4);
var yzm2=document.getElementById("yzm2");
document.codeimg.src="yzm.php?num="+num;
yzm2.value=num;
}
yzm.php代碼:
複製代碼 代碼如下:
header("Content-type: image/png");
$im=imagecreate(66,22); //建立畫布
$black=imagecolorallocate($im,0,0,0); //定義背景
$white=imagecolorallocate($im,255,255,255); //定義背景
$gray=imagecolorallocate($im,200,200,200); //定義背景
imagefill($im,0,0,$gray); //填充顏色
for($i=0;$i<4;$i++){ //定義4位隨機數
$str=mt_rand(1,5); //定義隨機字元所在位置的的Y座標
$size=mt_rand(6,9); //定義隨機字元的字型
$authnum=substr($_GET[num],$i,1); //擷取超級連結中傳遞的驗證碼
imagestring($im,$size,(3+$i*15),$str,$authnum,imagecolorallocate($im,rand(0,250),rand(0,250),rand(0,250)));//rand(0,500)數位模糊程度
} //水平輸出字串
for($i=0;$i<200;$i++){ //執行for迴圈,為驗證碼添加模糊背景
$randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); //建立背景
imagesetpixel($im,rand()%70,rand()%30,$randcolor); //繪製單一元素
}
imagepng($im); //產生png映像
imagedestroy($im); //銷毀映像
?>
注意:PHP需要配置才能執行相關方法。
運行效果:
http://www.bkjia.com/PHPjc/777638.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/777638.htmlTechArticlehtml代碼: 複製代碼 代碼如下: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http://...