帝國cms更換漂亮美觀的圖形驗證碼
老威在SEO這一行做了快兩年了,使用帝國cms大概一年半了,之前做的留言反饋都沒加過驗證碼,容易被惡意留言,最近新做的一套網站要用到這個功能,覺得帝國cms的圖形驗證碼有點扎眼,就順手在重新寫了下,外觀比之前漂亮,同時使用弧線和直線,增強了干擾能力;雙擊圖片可以不刷新頁面更換驗證碼,默認的需要刷新頁面。
使用方法:
1.將e文件直接復制到網站根目錄;
2.開啟 網站后臺->系統參數->用戶設置->會員登陸驗證碼和會員注冊驗證碼;
3.不要設置 網站后臺->系統參數->基本屬性->驗證碼配色 保持最初默認值即可;
4.(可選)設置完后,認證碼圖片會比input輸入框高可以用下面代碼替換 ,同時用onclick不刷新頁面更換驗證碼,兼容帝國7.0、7.2以及7.5版本。
模版->動態頁面模版管理->會員登錄頁面
源代碼:<img src="../../ShowKey/?v=login"/>
修改為:<img src="../../ShowKey/?v=login" onclick="javascript:this.src='../../ShowKey/?v=login&tm=+Math.random();'" style="vertical-align:middle"/> <span style="color:#666;vertical-align:bottom"> (點擊圖片更換)</span>
代碼下載:鏈接:https://pan.baidu.com/s/1xeU_LzSlk3CVuL9Fh41SCg 密碼:nzv0
貼出源代碼供學習交流:
<?php require('../class/connect.php'); //取得隨機數 function domake_password($pw_length){ global $public_r; if($public_r['keytog']==1)//字母 { $low_ascii_bound=65; $upper_ascii_bound=90; $notuse=array(91); } elseif($public_r['keytog']==2)//數字+字母 { $low_ascii_bound=50; $upper_ascii_bound=90; $notuse=array(58,59,60,61,62,63,64,73,79); } else//數字 { $low_ascii_bound=48; $upper_ascii_bound=57; $notuse=array(58); } while($i<$pw_length) { mt_srand((double)microtime()*1000000); $randnum=mt_rand($low_ascii_bound,$upper_ascii_bound); if(!in_array($randnum,$notuse)) { $password1=$password1.chr($randnum); $i++; } } return $password1; } //返回顏色 function ReturnShowKeyColor($img){ global $public_r; //背景色 if($public_r['keybgcolor']) { $bgcr=ToReturnRGB($public_r['keybgcolor']); $r['bgcolor']=imagecolorallocate($img,$bgcr[0],$bgcr[1],$bgcr[2]); } else { $r['bgcolor']=imagecolorallocate($img,245,rand(225,255),225); } //文字色 if($public_r['keyfontcolor']) { $fcr=ToReturnRGB($public_r['keyfontcolor']); $r['fontcolor']=ImageColorAllocate($img,$fcr[0],$fcr[1],$fcr[2]); } else { $r['fontcolor']=ImageColorAllocate($img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); } //干擾色 if($public_r['keydistcolor']) { $dcr=ToReturnRGB($public_r['keydistcolor']); $r['distcolor']=ImageColorAllocate($img,$dcr[0],$dcr[1],$dcr[2]); } else { $r['distcolor']=ImageColorAllocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); } return $r; } //顯示驗證碼 function ShowKey($v){ $vname=ecmsReturnKeyVarname($v); $key=strtolower(domake_password(4)); ecmsSetShowKey($vname,$key); //是否支持gd庫 if (function_exists("imagegif")) { header("Content-type: image/gif"); $img=imagecreate(80,26); $colorr=ReturnShowKeyColor($img); $bgcolor=$colorr['bgcolor']; $fontcolor=$colorr['fontcolor']; $distcolor=$colorr['distcolor']; imagefill($img,0,0,$bgcolor); for($i=0;$i<90;$i++) //加入干擾象素 { imagesetpixel($img,rand()%70,rand()%30,$distcolor); } for($i=0;$i<8;$i++){//加入干擾弧線 imagearc ($img,rand(0,360),rand(0,360),rand(200,350),rand(200,360),10,10,imagecolorallocate($img, rand(0,225), rand(0,225),rand(0,225))); } for($i=0;$i<4;$i++){//加入干擾直線 imageline ($img,rand(0,2),rand(0,27),rand(80,80),rand(1,28),imagecolorallocate($img, mt_rand(0,156),mt_rand(0,156),mt_rand(0,156))); } for($i=0;$i<4;$i++){ $charcolor=imagecolorallocate($img, mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); imagettftext ($img,rand(12,16),rand(-30,30),$i*20+2,rand(16,22),$charcolor,"STENCIL.TTF",$key[$i]); } imagegif($img); imagedestroy($img); } elseif(function_exists("imagejpeg")) { header ("Content-type: image/jpeg"); $img=imagecreate(80,26); $colorr=ReturnShowKeyColor($img); $bgcolor=$colorr['bgcolor']; $fontcolor=$colorr['fontcolor']; $distcolor=$colorr['distcolor']; imagefill($img,0,0,$bgcolor); for($i=0;$i<90;$i++) //加入干擾象素 { imagesetpixel($img,rand()%70,rand()%30,$distcolor); } for($i=0;$i<8;$i++){//加入干擾弧線 imagearc ($img,rand(0,360),rand(0,360),rand(200,350),rand(200,360),10,10,imagecolorallocate($img, rand(0,225), rand(0,225),rand(0,225))); } for($i=0;$i<4;$i++){//加入干擾直線 imageline ($img,rand(0,2),rand(0,27),rand(80,80),rand(1,28),imagecolorallocate($img, mt_rand(0,156),mt_rand(0,156),mt_rand(0,156))); } for($i=0;$i<4;$i++){ $charcolor=imagecolorallocate($img, mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); imagettftext ($img,rand(12,16),rand(-30,30),$i*20+2,rand(16,22),$charcolor,"STENCIL.TTF",$key[$i]); } imagejpeg($img); imagedestroy($img); } elseif (function_exists("imagepng")) { header ("Content-type: image/png"); $img=imagecreate(80,26); $colorr=ReturnShowKeyColor($img); $bgcolor=$colorr['bgcolor']; $fontcolor=$colorr['fontcolor']; $distcolor=$colorr['distcolor']; imagefill($img,0,0,$bgcolor); for($i=0;$i<90;$i++) //加入干擾象素 { imagesetpixel($img,rand()%70,rand()%30,$distcolor); } for($i=0;$i<8;$i++){//加入干擾弧線 imagearc ($img,rand(0,360),rand(0,360),rand(200,350),rand(200,360),10,10,imagecolorallocate($img, rand(0,225), rand(0,225),rand(0,225))); } for($i=0;$i<4;$i++){//加入干擾直線 imageline ($img,rand(0,2),rand(0,27),rand(80,80),rand(1,28),imagecolorallocate($img, mt_rand(0,156),mt_rand(0,156),mt_rand(0,156))); } for($i=0;$i<4;$i++){ $charcolor=imagecolorallocate($img, mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); imagettftext ($img,rand(12,16),rand(-30,30),$i*20+2,rand(16,22),$charcolor,"STENCIL.TTF",$key[$i]); } imagepng($img); imagedestroy($img); } elseif (function_exists("imagewbmp")) { header ("Content-type: image/vnd.wap.wbmp"); $img=imagecreate(80,26); $colorr=ReturnShowKeyColor($img); $bgcolor=$colorr['bgcolor']; $fontcolor=$colorr['fontcolor']; $distcolor=$colorr['distcolor']; imagefill($img,0,0,$bgcolor); for($i=0;$i<90;$i++) //加入干擾象素 { imagesetpixel($img,rand()%70,rand()%30,$distcolor); } for($i=0;$i<8;$i++){//加入干擾弧線 imagearc ($img,rand(0,360),rand(0,360),rand(200,350),rand(200,360),10,10,imagecolorallocate($img, rand(0,225), rand(0,225),rand(0,225))); } for($i=0;$i<4;$i++){//加入干擾直線 imageline ($img,rand(0,2),rand(0,27),rand(80,80),rand(1,28),imagecolorallocate($img, mt_rand(0,156),mt_rand(0,156),mt_rand(0,156))); } for($i=0;$i<4;$i++){ $charcolor=imagecolorallocate($img, mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); imagettftext ($img,rand(12,16),rand(-30,30),$i*20+2,rand(16,22),$charcolor,"STENCIL.TTF",$key[$i]); } imagewbmp($img); imagedestroy($img); } else { ecmsSetShowKey($vname,'ecms'); echo ReadFiletext("../data/images/ecms.gif"); } } //返回變量名 function ecmsReturnKeyVarname($v){ if($v=='login')//登陸 { $name='checkloginkey'; } elseif($v=='reg')//注冊 { $name='checkregkey'; } elseif($v=='info')//信息 { $name='checkinfokey'; } elseif($v=='spacefb')//空間反饋 { $name='checkspacefbkey'; } elseif($v=='spacegb')//空間留言 { $name='checkspacegbkey'; } elseif($v=='gbook')//留言 { $name='checkgbookkey'; } elseif($v=='feedback')//反饋 { $name='checkfeedbackkey'; } elseif($v=='getpassword')//取回密碼 { $name='checkgetpasskey'; } elseif($v=='regsend')//重發激活郵件 { $name='checkregsendkey'; } else//評論pl { $name='checkplkey'; } return $name; } $v=$_GET['v']; ShowKey($v); ?>