PHP+javascript製作帶提示的驗證碼源碼分享_php執行個體

來源:互聯網
上載者:User

html代碼:

複製代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>簡單驗證碼</title>
</head>
<script language="javascript" src="js/checked.js"></script>
<body>
<form id="register" name="register" method="post" >
<table align="center">
    <tr>
      <td ><div align="right">驗證碼:</div></td>
      <td ><input id="yzm" type="text" name="yzm" size="8" onBlur="javascript:chkyzm(form)" onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"/></td><td>
        <input id="yzm2" type="hidden" value="" /></td>
      <td align="center" valign="middle"><script>yzm()</script></td>
      <td ><a href="javascript:code()" style="text-decoration:none">看不清</a></td>
      <td width="150"  align="center"><div id="yzm1"><font color="#999999">請輸入驗證碼</font></div></td>
    </tr> 
</table>
  </form>
</body>
</html>

JS代碼:

複製代碼 代碼如下:

function chkyzm(form){     //對驗證碼進行驗證
 if(form.yzm.value==""){
  yzm1.innerHTML="<font color=#FF0000>×驗證碼不可為空</font>";
 }else if(form.yzm.value!=form.yzm2.value){
  yzm1.innerHTML="<font color=#FF0000>×驗證碼輸入錯誤</font>";
 }else{
  yzm1.innerHTML="<font color=green>√驗證碼輸入正確</font>";
 }
}
function yzm(){      //產生驗證碼
 var num1=Math.round(Math.random()*1000000);//隨機小數放大
 var num=num1.toString().substr(0,4);//取4位整數
 var yzm2=document.getElementById("yzm2");
 document.write("<img name=codeimg src=yzm.php?num="+num+"'>");
 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代碼:

複製代碼 代碼如下:

<?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需要配置才能執行相關方法。

運行效果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.