|
我是PHP初學(xué)者,最近看了php100教程視頻,參考了網(wǎng)上的一些資料,做了一個簡簡單單的php類封裝驗證碼,跟大家分享一下。 verification_code.php <?php /****文檔注釋********************************************************* 描述:創(chuàng)建驗證碼圖片 *********************************************************************/ session_start(); /********************************************************************* 實例化驗證碼類VerificationCodeImage 輸入:,,$mode=7 $v_num=4 $img_w=65 $img_h=20 $int_pixel_num=80 $int_line_num=5 $font_dir="fonts/" 輸出:,,驗證碼圖像 *********************************************************************/ $vi,=,new,VerificationCodeImage(7,4,65,20,80,5,"fonts/"); class,VerificationCodeImage { private,$mode;, //1:數(shù)字模式,2:字母模式,3:數(shù)字字母模式,4:漢字模式,5:數(shù)字漢字模式,6:字母漢字模式,7:數(shù)字字母漢字模式 private,$v_num;, //驗證碼個數(shù) private,$img_w;, //驗證碼圖像寬度 private,$img_h;, //驗證碼圖像高度 private,$int_pixel_num;,//干擾像素個數(shù) private,$int_line_num;, //干擾線條數(shù) private,$font_dir;,,, //字體文件相對路徑 /********************************************************************* 函數(shù)名:__construct 描述:,,初始化類VerificationCodeImage 輸入:,,$mode=驗證碼模式 $v_num=驗證碼個數(shù) $img_w=驗證碼寬度 $img_h=驗證碼高度 $int_pixel_num=干擾像素個數(shù) $int_line_num=干擾線條數(shù) $font_dir=驗證碼字體所在目錄 輸出:,,驗證碼圖像 *********************************************************************/ function,__construct,($mode,$v_num,$img_w,$img_h,$int_pixel_num,$int_line_num,$font_ dir) { $this->mode,=,$mode; $this->v_num,=,$v_num; $this->img_w,=,$img_w; $this->img_h,=,$img_h; $this->int_pixel_num,=,$int_pixel_num; $this->int_line_num,=,$int_line_num; $this->font_dir,=,$font_dir; $this->CreateImage(); } /********************************************************************* 函數(shù)名:GetCodeChar 描述:,,獲得驗證碼內(nèi)容 輸入:,,$mode=驗證碼模式 輸出:,,驗證碼內(nèi)容 返回值:$ychar=驗證碼內(nèi)容 *********************************************************************/ function,GetCodeChar,($mode) { if,($mode,==,"1") { $ychar,=,"0,1,2,3,4,5,6,7,8,9"; } elseif,($mode,==,"2") { $ychar,=,"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,"; $ychar.="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; } elseif,($mode,==,"3") { $ychar,=,"0,1,2,3,4,5,6,7,8,9,"; $ychar.=,"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,"; $ychar.="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; } elseif,($mode,==,"4") { $ychar,=,"一,二,三,四,五,六,七,八,九,十,個,百,千,萬,零,億,小,中,大,多,少,高, 低"; } elseif,($mode,==,"5") { $ychar,=,"0,1,2,3,4,5,6,7,8,9,"; $ychar,.=,"一,二,三,四,五,六,七,八,九,十,個,百,千,萬,零,億,小,中,大,多,少, 高,低"; } elseif,($mode,==,"6") { $ychar,=,"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,"; $ychar.="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,"; $ychar,.=,"一,二,三,四,五,六,七,八,九,十,個,百,千,萬,零,億,小,中,大,多,少, 高,低"; } elseif,($mode,==,"7") { $ychar,=,"0,1,2,3,4,5,6,7,8,9,"; $ychar.=,"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,"; $ychar.="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,"; $ychar,.=,"一,二,三,四,五,六,七,八,九,十,個,百,千,萬,零,億,小,中,大,多,少, 高,低"; } return,$ychar; } /********************************************************************* 函數(shù)名:RandColor 描述:,,獲得三原色 輸入:,,$rs=紅 $re=紅 $gs=綠 $ge=綠 $bs=藍 $be=藍 $rs,$re,$gs,$ge,$bs,$be均為數(shù)字,取值范圍為[0,255] 輸出:,,三原色 返回值:array($r,$g,$b) *********************************************************************/ function,RandColor,($rs,$re,$gs,$ge,$bs,$be) { $r,=,mt_rand($rs,$re); $g,=,mt_rand($gs,$ge); $b,=,mt_rand($bs,$be); return,array($r,$g,$b); } /********************************************************************* 函數(shù)名:CreateImage 描述:,,創(chuàng)建驗證碼圖片 輸出:,,驗證碼圖片 *********************************************************************/ function,CreateImage,() { $im,=,imagecreate($this->img_w,$this->img_h); $bgcolor,=,imagecolorallocate($im,163,211,156); $black,=,imagecolorallocate($im,,0,0,0); $white,=,imagecolorallocate($im,,255,255,255);, /* scandir,--,列出指定路徑中的文件和目錄 array,scandir,(,string,directory,[,,int,sorting_order,[,,resource,context]],) 參數(shù)說明: directory:要被瀏覽的目錄, sorting_order:默認的排序順序是按字母升序排列。如果使用了可選參數(shù),sorting_order(設(shè)為,1),則排序順序是按字母降序排列。 返回一個,array,包含有,directory,中的文件和目錄。 例: fonts文件夾與當前網(wǎng)頁是同一個層次的,fonts下面包含一個simhei.ttf print_r(scandir("fonts/"));返回Array,(,[0],=>,.,[1],=>,..,[2],=>,simhei.ttf,) */ $fonts,=,scandir($this->font_dir);, $fmax,=,count($fonts),-,2;,//排除Array[0]=>.和Array[1]=>.. $ychar,=,$this->GetCodeChar($this->mode); /* explode,--,使用一個字符串分割另一個字符串 array,explode,(,string,separator,,string,string,[,,int,limit],) 參數(shù)說明: separator:分隔符 string:被分割的字符串 limit,:最多返回的數(shù)組元素個數(shù) 返回由分隔符分割后的字符串組成的數(shù)組 */ $list,=,explode(",",$ychar); /* mt_rand,--,生成更好的隨機數(shù) int,mt_rand,(,[int,min,,int,max],) */ $x,=,mt_rand(2,$this->img_w/($this->v_num+2)); $cmax,=,count($list),-,1; $v_code,=,''; for,($i=0;$i<$this->v_num;$i++), //驗證碼 { $randnum,=,mt_rand(0,$cmax); , $this_char,=,$list[$randnum]; , $v_code,.=,$this_char; , $size,=,mt_rand(intval($this->img_w/5),intval($this->img_w/4));,//獲取字體大小 , $angle,=,mt_rand(-20,20);,//獲取角度 , $y,=,mt_rand(($size+2),($this->img_h-2));,//設(shè)定了字體基線的位置,不是字符的最底端。 ,,, $rand_color,=,$this->RandColor(0,200,0,100,0,250); ,,, $randcolor,=,imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]); ,,, $fontrand,=,mt_rand(2,,$fmax); $font,=,"$this->font_dir/".$fonts[$fontrand];,//獲取字體類型 ,,, //imagettftext($im,,$size,,$angle,,$x,,$y,,$randcolor,,$font,,$this_char); imagettftext($im,,$size,,$angle,,$x,,$y,,$randcolor,,$font,,iconv("GB2312","UTF-8",$this_char)); ,,, $x,=,$x,+,intval($this->img_w/($this->v_num+1)); } for,($i=0;$i<$this->int_pixel_num;$i++),//干擾像素 { ,,, $rand_color,=,$this->RandColor(50,250,0,250,50,250); ,,, $rand_color_pixel,=,imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]); ,,, imagesetpixel($im,,mt_rand()%$this->img_w,,mt_rand()%$this->img_h,,$rand_color_pixel); } for,($i=0;$i<$this->int_line_num;$i++) //干擾線 {, ,,, $rand_color,=,$this->RandColor(0,250,0,250,0,250); ,,, $rand_color_line,=,imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]); ,,, imageline($im,,mt_rand(0,intval($this->img_w/3)),,mt_rand(0,$this->img_h),,mt_rand(intval($this->img_w,-,($this->img_w/3)),$this->img_w),,mt_rand(0,$this->img_h),,$rand_color_line); } imageantialias($im,true);,//抗鋸齒 $_SESSION[v_code],=,$v_code;,//用$_SESSION[v_code]保存驗證碼 //生成圖像 if,(function_exists("imagegif")), { header,("Content-type:,image/gif"); imagegif($im); } elseif,(function_exists("imagepng")), { header,("Content-type:,image/png"); imagepng($im); } elseif,(function_exists("imagejpeg")), { header,("Content-type:,image/jpeg"); imagejpeg($im); } elseif,(function_exists("imagewbmp")), { header,("Content-type:,image/vnd.wap.wbmp"); imagewbmp($im); } else { die("本機不支持生成圖像!"); } imagedestroy($im); } } ?> index.php <?php session_start(); if($_POST[submit]){ if($_POST[v_code]!=$_SESSION[v_code]){ echo,"<script>alert('驗證碼輸入錯誤~');</script>"; }else{ echo,"<script>alert('驗證碼輸入正確~');</script>"; } } ?> <!DOCTYPE,html,PUBLIC,"-//W3C//DTD,XHTML,1.0,Transitional//EN"," R/xhtml1/DTD/xhtml1-transitional.dtd"> <html,xmlns=";> <head> <meta,http-equiv="Content-Type",content="text/html;,charset=gb2312",/> <title>驗證碼</title> <script,language="javascript"> function,chkv_code(){ if(document.getElementById("v_code").value==""){ alert("請輸入驗證碼~"); document.getElementById("v_code").focus(); return,false; } return,true; } </script> </head> <body> <form,method="post",onsubmit="return,chkv_code();"> <div,align="center"> 驗證碼:<input,type="text",name="v_code",size="8",maxlength="4"> <img,src="verification_code.php",alt="看不清,點此刷新驗證碼 ",name="verify_code",width="65",height="20",border="0",id="verify_code",onclick="document. getElementById('verify_code').src='verification_code.php?'+Math.random();",style="cursor:point er;",/> <p><input,type="submit",value="提 交",name="submit"> </div> </form> </body>
信息發(fā)布:廣州名易軟件有限公司 http://www.jetlc.com
|