我們在實際開發中,會用到短信驗證碼以及網頁驗證碼,相對來說網頁驗證碼的成本比較低,安全系數也還可以,接下來我們就來做一個網頁端的驗證碼,直接上代碼:
首先來說一下文件目錄結構:
|----------phpyanzheng 項目文件夾
|----fonttype 文件夾放字體文件,字體文件一般以 .ttf和.otf結尾
|----1.php 里面放php語言代碼
|----index.html 里面放網頁布局
1.php
<?php header('content-type:image/jpeg');//定義一下代碼以jpeg文件來解析 $width = 120;//定義了圖像的寬 $height = 40;//定義了圖像的高 $element = array('q','w','e','r','t','y','u','i','o','p','l','k','j','h','g','f','d','s','a','z','x', 'c','v','b','n','m','1','2','3','4','5','6','7','8','9','0','Q','W','E','R','T','Y','U','I','O','P','A' ,'S','D','F','G','H','J','K','L','M','N','B','V','C','X','Z');//定義一個顯示文本的數組 $string = ''; for($i=0;$i<4;$i++){ $string.=$element[rand(0,count($element)-1)];//隨機產生四個文本目標 } $img = imagecreatetruecolor($width, $height);//設置驗證區寬高 $colorBg = imagecolorallocate($img,rand(185,255),rand(185,255),rand(185,255));//產生200-255的隨機數 $colorBorder = imagecolorallocate($img,rand(50,100),rand(50,100),rand(50,100));//邊框顏色 $colorDian = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));//背景小點的顏色 $colorLine = imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255)); $colorString = imagecolorallocate($img,rand(20,80),rand(20,80),rand(20,80)); imagefill($img, 0, 0, $colorBg);//設置位置和背景顏色 imagerectangle($img,0,0,$width-1,$height-1,$colorBorder);//畫一個邊框 for($i=0;$i<200;$i++){//循環出200個干擾點 imagesetpixel($img, rand(0, $width-1), rand(0, $height-1), $colorDian); } for($i=0;$i<round(5);$i++){//循環出5條干擾線 imageline($img,rand(0,$width/2),rand(0,$height/2),rand($width/2,$width),rand($height/2,$height),$colorLine); } //imagestring($img, 4, 0, 0,'a5dg', $colorString); imagettftext($img,25,rand(5, 15),rand(0,40),39,$colorString,'fonttype/PrincetownStd.otf',$string); // 圖像名稱 字體大小 傾斜角度 起始位置X軸 起始位置Y軸 顏色 字體位置 顯示的內容 imagejpeg($img);//輸出圖像 ?>
index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>孫三峰--驗證碼</title> <script type="text/javascript" src="js/jquery-1.1.0.js" ></script> </head> <body> <!--注意:原文中對應的ip地址是自己電腦端ip,如果不能正常運行,請修改ip--> <p> <label class="lbright">驗證碼:</label> <span> <input type="text" name="validcode" style="width:70px; vertical-align:middle;" id="validcode"/> <img id="codePic" src="http://192.168.1.113:8601/PHP/phpyanzheng/1.php" width="120" height="40"/> </span> <span id="codePic" onclick="getPic();">看不清楚,換一張</span> <!----> </p> </body> <script type="text/javascript"> function getPic(){ $("#codePic").attr("src","http://192.168.1.113:8601/PHP/phpyanzheng/1.php?flag="+Math.random()); <!--局部刷新--> }; </script> </html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。