這篇文章主要為大家詳細介紹了php映像驗證碼的產生代碼,具有一定的參考價值,感興趣的小夥伴們可以參考一下
本文執行個體為大家分享了php封裝的一個產生映像驗證碼,供大家參考,具體內容如下
一、代碼
index..php
<!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=utf-8" /> <title>js+GD2函數製作無重新整理驗證碼</title> <link rel="stylesheet" type="text/css" href="css/style.css" rel="external nofollow" > </head> <body> <p style="width: 750px; border: 1px solid #0D6C8C; text-align: left"> <p style="width:100%; height:25px; border:1px #FFFFFF solid; background:url(img/t-background.gif); color:#FFFFFF; padding-top:5px; padding-left:20px; text-align:left">填寫使用者註冊資訊</p> <p style="width:400px; text-align:left; padding-left:50px; padding-top:15px; float:left"> <script language="javascript"> function chkinput(form) { if (form.username.value==''){ alert('請輸入使用者名稱!'); form.username.focus(); return false; } if (form.password.value==''){ alert('請輸入註冊密碼!'); form.password.focus(); return false; } if (form.defPassword.value==''){ alert('請輸入確認密碼!'); form.defPassword.focus(); return false; } if (form.password.value.length<6){ alert('密碼長度應大於6位!'); form.password.focus(); return false; } if (form.password.value!=form.defPassword.value){ alert('密碼與確認密碼不同!'); form.password.focus(); return false; } if (form . email . value == "") { alert("請輸入E-mail地址!"); form . email . select(); return (false); } var i= form . email . value . indexOf("@"); if (i < 0) { alert("請輸入正確的E-mail地址!"); form . email . select(); return (false); } if (form . tel . value == "") { alert('請輸入電話號碼!'); form . tel . focus(); return (false); } var t=/^([0-9]|[\-])+$/g; if (! t . test(form . tel . value) || form . tel . value . length > 18 || form . tel . value . length < 7) { alert('電話格式輸入有誤!'); form . tel . focus(); return (false); } if (form.address.value==''){ alert('請輸入聯絡地址!'); form.address.focus(); return false; } if (form.validatorCode.value==''){ alert('請輸入驗證碼!'); form.validatorCode.focus(); return false; } if (form.validatorCode.value!=form.defValidatorCode.value) { alert('驗證碼輸入有誤!'); form.validatorCode.focus(); return false; } return true; } </script> <form name="form1" method="post" action="success.php" onSubmit="return chkinput(this)"> 使用者名稱: <input type="text" name="username" size="30" class="input" /><br><br> 密 碼: <input type="password" name="password" size="30" class="input" /><br><br> 確認密碼:<input type="password" name="defPassword" size="30" class="input"><br><br> E-mail: <input type="text" name="email" size="30" class="input" /><br><br> 電話: <input type="text" name="tel" size="30" class="input" /><br><br> 聯絡地址:<input type="text" name="address" size="30" class="input" /><br><br> 驗證碼: <input type="text" name="validatorCode" size="10" class="input" /> <input type="hidden" name="defValidatorCode" value="" /> <script language="javascript"> var num1=Math.round(Math.random()*10000000); var num=num1.toString().substr(0,4); document.write("<img name=codeimg src='TestCode.php?code="+num+"'>"); form1.defValidatorCode.value=num; function reCode(){ var num1=Math.round(Math.random()*10000000); var num=num1.toString().substr(0,4); document.codeimg.src="TestCode.php?code="+num; form1.defValidatorCode.value=num; } </script> <a href="javascript:reCode()" rel="external nofollow" class="a1">看不清</a> <br><br> <input type="submit" value="提交"> <input type="reset" value=" 重寫"> </form> </p> <p style="width:300px; float:right; padding-top:20px"> <p style="width:285px; height:250px; border:1px dotted #999999; background:#E9F2F7; OVERFLOW-X: auto; OVERFLOW-Y:auto; text-align:left; line-height:20px"> 使用者在使用***網站網路服務過程中,必須遵循以下原則:<br /> 1、遵守中國有關的法律和法規。不得利用本站危害國家安全、泄露國家秘密,不得侵犯國家社會集體的和公民的合法權益。不得利用***網站網路服務系統進行任何不利於***網站的行為。<br /> 2、遵守所有與網路服務有關的網路通訊協定、規定和程式。<br /> 3、不干擾或混亂網路服務。<br /> 4、不得為任何非法目的而使用本網路服務系統。<br /> 5、不得傳播任何虛假的、騷擾性的、中傷他人的、辱罵性的、恐嚇性的、庸俗的或其他任何非法的資訊資料。<br /> 6、不得侵犯其他任何第三方的專利權、著作權、商標權、名譽權或其他任何合法權益。<br /> 7、不得在論壇BBS或留言簿發表任何與政治相關的資訊。 <br /> 8、遵守所有使用網站服務的網路通訊協定、規定、程式和慣例。<br /> 9、如發現任何非法使用使用者帳號,應立即通告***網站。<br /> 10、如使用者在使用網路服務時違反上述任何規定,***網站有權要求使用者改正或直接取消該帳號並停止為該使用者提供相關的網路服務。<br /> </p> </p> </p> <?php require_once 'footer.php'; ?> </body> </html>
TestCode.php
<?php class TestCode{//建立類名為TestCode private $width; private $height; private $str; private $im; private $strColor; function construct($width,$height){ $this->width=$width; $this->height=$height; $this->str=$_GET['code']; $this->createImage(); } function createImage(){ $this->im=imagecreate($this->width,$this->height);//建立畫布 imagecolorallocate($this->im,200,200,200);//為畫布添加顏色 for($i=0;$i<4;$i++){//迴圈輸出四個數字 $this->strColor=imagecolorallocate($this->im,rand(0,100),rand(0,100),rand(0,100)); imagestring($this->im,rand(3,5),$this->width/4*$i+rand(5,10),rand(2,5),$this->str[$i],$this->strColor); } for($i=0;$i<200;$i++){//迴圈輸出200個像素點 $this->strColor=imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($this->im,rand(0,$this->width),rand(0,$this->height),$this->strColor); } } function show(){// header('content-type:image/png');//定義輸出為映像類型 imagepng($this->im);//產生映像 imagedestroy($this->im);//銷毀映像釋放記憶體 } } $image=new TestCode(80,20);//將類執行個體化為對象 $image->show();//調用函數 ?>
二、運行結果