| jackli000   
         
  
 | 分享:                 
 [分享] 【官方】基於Discuz! 7.2的隨機位數驗證碼
                           (GBK-簡)
                      
                        
                        
                          |  x0 | 
 
   【插件資訊】插件名稱:【官方】基於Discuz! 7.2的隨機位數驗證碼   適用版本:其他 語言編碼:GBK-簡 插件作者:Viswow 版權所屬:Discuz 原帖連結:http://www.discuz.net/thr...1-1-1.html【插件說明】 鑑於註冊機的氾濫,現在修改Discuz驗證碼為隨機位數驗證碼,本修改在驗證模式為:一般的情況下可用,高級驗證模式只暫時將驗證碼修改為了5位,由於高級驗證碼需要修改的文件更多,所以在觀察這個修改的效果之後再決定是否進行修改。修改後,如果啟用了驗證碼隨機背景圖片,那麼請在images\seccode\background中將背景圖加長,否則在顯示過長驗證碼的時候會顯示的比較奇怪。 GBK版本的修改文件包,UTF8請對應修改。 修改後的效果為:註冊的時候驗證碼隨機為2-7位。 效果示例:       後台設置示例:     另外建議大家自行更換images/fonts/en目錄中的字體。能夠有效的避免驗證碼被識別。 注意:修改文件較多,修改前請自行備份。 有問題請短消息我。 1.打開templates/default/seccheck.htm 找到 
 if(!(/[0-9A-Za-z]{4}/.test(seccodeverify))) {複製代碼 修改為: 
 if(!(/[0-9A-Za-z]{1,}/.test(seccodeverify))) {複製代碼 2.打開/ajax.php  找到 
 $seccode = random(6, 1);複製代碼 修改為: 
 $seccode = random($seccodelength*3, 1);複製代碼 3.打開include\seccode.class.php  找到: 
         function display() {複製代碼$this->type == 2 && !extension_loaded('ming') && $this->type = 0;
 $this->width = $this->width >= 100 && $this->width <= 200 ? $this->width : 150;
 $this->height = $this->height >= 50 && $this->height <= 80 ? $this->height : 60;
 seccodeconvert($this->code);
 if($this->type < 2 && function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') &&
 function_exists('imagecolorallocate') && function_exists('imagechar') && function_exists('imagecolorsforindex') &&
 function_exists('imageline') && function_exists('imagecreatefromstring') && (function_exists('imagegif') || function_exists('imagepng') || function_exists('imagejpeg'))) {
 $this->image();
 } elseif($this->type == 2 && extension_loaded('ming')) {
 $this->flash();
 } elseif($this->type == 3) {
 $this->audio();
 } else {
 $this->bitmap();
 }
 }
 修改為: 
         function display() {複製代碼global $seccodelength;
 $this->type == 2 && !extension_loaded('ming') && $this->type = 0;
 $this->width = $seccodelength*50;
 $this->height = $this->height >= 50 && $this->height <= 80 ? $this->height : 60;
 seccodeconvert($this->code);
 if($this->type < 2 && function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') &&
 function_exists('imagecolorallocate') && function_exists('imagechar') && function_exists('imagecolorsforindex') &&
 function_exists('imageline') && function_exists('imagecreatefromstring') && (function_exists('imagegif') || function_exists('imagepng') || function_exists('imagejpeg'))) {
 $this->image();
 } elseif($this->type == 2 && extension_loaded('ming')) {
 $this->flash();
 } elseif($this->type == 3) {
 $this->audio();
 } else {
 $this->bitmap();
 }
 }
 找到 
 imagecopymerge($this->im, $imwm, 0, 0, mt_rand(0, 200 - $this->width), mt_rand(0, 80 - $this->height), imageSX($imwm), imageSY($ imwm), 100);複製代碼 修改為: 
         imagecopymerge($this->im, $imwm, 0, 0, mt_rand(0, 300- $this->width), mt_rand(0, 80 - $this->height), imageSX($imwm), imageSY($ imwm), 100);複製代碼 找到: 
         function ttffont() {複製代碼$seccode = $this->code;
 $charset = $GLOBALS['charset'];
 $seccoderoot = $this->type ? $this->fontpath.'ch/' : $this->fontpath.'en/';
 $dirs = opendir($seccoderoot);
 $seccodettf = array();
 while($entry = readdir($dirs)) {
 if($entry != '.' && $entry != '..' && in_array(strtolower(fileext($entry)), array('ttf', 'ttc'))) {
 $seccodettf[] = $entry;
 }
 }
 if(empty($seccodettf)) {
 $this->giffont();
 return;
 }
 $seccodelength = 4;
 if($this->type && !empty($seccodettf)) {
 if(strtoupper($charset) != 'UTF-8') {
 include $this->includepath.'chinese.class.php';
 $cvt = new Chinese($charset, 'utf8');
 $seccode = $cvt->Convert($seccode);
 }
 $seccode = array(substr($seccode, 0, 3), substr($seccode, 3, 3));
 $seccodelength = 2;
 }
 $widthtotal = 0;
 for($i = 0; $i < $seccodelength; $i++) {
 $font[$i]['font'] = $seccoderoot.$seccodettf[array_rand($seccodettf)];
 $font[$i]['angle'] = $this->angle ? mt_rand(-30, 30) : 0;
 $font[$i]['size'] = $this->type ? $this->width / 7 : $this->width / 6;
 $this->size && $font[$i]['size'] = mt_rand($font[$i]['size'] - $this->width / 40, $font[$i]['size' ] + $this->width / 20);
 $box = imagettfbbox($font[$i]['size'], 0, $font[$i]['font'], $seccode[$i]);
 $font[$i]['zheight'] = max($box[1], $box[3]) - min($box[5], $box[7]);
 $box = imagettfbbox($font[$i]['size'], $font[$i]['angle'], $font[$i]['font'], $seccode[$i]);
 $font[$i]['height'] = max($box[1], $box[3]) - min($box[5], $box[7]);
 $font[$i]['hd'] = $font[$i]['height'] - $font[$i]['zheight'];
 $font[$i]['width'] = (max($box[2], $box[4]) - min($box[0], $box[6])) + mt_rand(0, $this ->width / 8);
 $font[$i]['width'] = $font[$i]['width'] > $this->width / $seccodelength ? $this->width / $seccodelength : $font[$i][' width'];
 $widthtotal += $font[$i]['width'];
 }
 $x = mt_rand($font[0]['angle'] > 0 ? cos(deg2rad(90 - $font[0]['angle'])) * $font[0]['zheight'] : 1, $this->width - $widthtotal);
 !$this->color && $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
 for($i = 0; $i < $seccodelength; $i++) {
 if($this->color) {
 $this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
 $this->shadow && $text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]) ;
 $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
 } elseif($this->shadow) {
 $text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);
 }
 $y = $font[0]['angle'] > 0 ? mt_rand($font[$i]['height'], $this->height) : mt_rand($font[$i]['height'] - $font[$i]['hd'], $this->height - $font[$i]['hd']);
 $this->shadow && imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x + 1, $y + 1, $text_shadowcolor , $font[$i]['font'], $seccode[$i]);
 imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x, $y, $text_color, $font[$i]['font '], $seccode[$i]);
 $x += $font[$i]['width'];
 }
 }
 修改為: 
         function ttffont() {複製代碼$seccode = $this->code;
 $charset = $GLOBALS['charset'];
 $seccoderoot = $this->type ? $this->fontpath.'ch/' : $this->fontpath.'en/';
 $dirs = opendir($seccoderoot);
 $seccodettf = array();
 while($entry = readdir($dirs)) {
 if($entry != '.' && $entry != '..' && in_array(strtolower(fileext($entry)), array('ttf', 'ttc'))) {
 $seccodettf[] = $entry;
 }
 }
 if(empty($seccodettf)) {
 $this->giffont();
 return;
 }
 $seccodelength = 4;
 if($this->type && !empty($seccodettf)) {
 if(strtoupper($charset) != 'UTF-8') {
 include $this->includepath.'chinese.class.php';
 $cvt = new Chinese($charset, 'utf8');
 $seccode = $cvt->Convert($seccode);
 }
 $seccode = array(substr($seccode, 0, 3), substr($seccode, 3, 3));
 $seccodelength = 2;
 }
 $widthtotal = 0;
 for($i = 0; $i < $seccodelength; $i++) {
 $font[$i]['font'] = $seccoderoot.$seccodettf[array_rand($seccodettf)];
 $font[$i]['angle'] = $this->angle ? mt_rand(-30, 30) : 0;
 $font[$i]['size'] = $this->type ? $this->width / 7 : $this->width / 6;
 $this->size && $font[$i]['size'] = mt_rand($font[$i]['size'] - $this->width / 40, $font[$i]['size' ] + $this->width / 20);
 $box = imagettfbbox($font[$i]['size'], 0, $font[$i]['font'], $seccode[$i]);
 $font[$i]['zheight'] = max($box[1], $box[3]) - min($box[5], $box[7]);
 $box = imagettfbbox($font[$i]['size'], $font[$i]['angle'], $font[$i]['font'], $seccode[$i]);
 $font[$i]['height'] = max($box[1], $box[3]) - min($box[5], $box[7]);
 $font[$i]['hd'] = $font[$i]['height'] - $font[$i]['zheight'];
 $font[$i]['width'] = (max($box[2], $box[4]) - min($box[0], $box[6])) + mt_rand(0, $this ->width / 8);
 $font[$i]['width'] = $font[$i]['width'] > $this->width / $seccodelength ? $this->width / $seccodelength : $font[$i][' width'];
 $widthtotal += $font[$i]['width'];
 }
 $x = mt_rand($font[0]['angle'] > 0 ? cos(deg2rad(90 - $font[0]['angle'])) * $font[0]['zheight'] : 1, $this->width - $widthtotal);
 !$this->color && $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
 for($i = 0; $i < $seccodelength; $i++) {
 if($this->color) {
 $this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
 $this->shadow && $text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]) ;
 $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
 } elseif($this->shadow) {
 $text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);
 }
 $y = $font[0]['angle'] > 0 ? mt_rand($font[$i]['height'], $this->height) : mt_rand($font[$i]['height'] - $font[$i]['hd'], $this->height - $font[$i]['hd']);
 $this->shadow && imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x + 1, $y + 1, $text_shadowcolor , $font[$i]['font'], $seccode[$i]);
 imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x, $y, $text_color, $font[$i]['font '], $seccode[$i]);
 $x += $font[$i]['width'];
 }
 }
 找到 
         function giffont() {複製代碼$seccode = $this->code;
 $seccodedir = array();
 if(function_exists('imagecreatefromgif')) {
 $seccoderoot = $this->datapath.'gif/';
 $dirs = opendir($seccoderoot);
 while($dir = readdir($dirs)) {
 if($dir != '.' && $dir != '..' && file_exists($seccoderoot.$dir.'/9.gif')) {
 $seccodedir[] = $dir;
 }
 }
 }
 $widthtotal = 0;
 for($i = 0; $i <= 3; $i++) {
 $this->imcodefile = $seccodedir ? $seccoderoot.$seccodedir[array_rand($seccodedir)].'/'.strtolower($seccode[$i]).'.gif' : '';
 if(!empty($this->imcodefile) && file_exists($this->imcodefile)) {
 $font[$i]['file'] = $this->imcodefile;
 $font[$i]['data'] = getimagesize($this->imcodefile);
 $font[$i]['width'] = $font[$i]['data'][0] + mt_rand(0, 6) - 4;
 $font[$i]['height'] = $font[$i]['data'][1] + mt_rand(0, 6) - 4;
 $font[$i]['width'] += mt_rand(0, $this->width / 5 - $font[$i]['width']);
 $widthtotal += $font[$i]['width'];
 } else {
 $font[$i]['file'] = '';
 $font[$i]['width'] = 8 + mt_rand(0, $this->width / 5 - 5);
 $widthtotal += $font[$i]['width'];
 }
 }
 $x = mt_rand(1, $this->width - $widthtotal);
 for($i = 0; $i <= 3; $i++) {
 $this->color && $this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
 if($font[$i]['file']) {
 $this->imcode = imagecreatefromgif($font[$i]['file']);
 if($this->size) {
 $font[$i]['width'] = mt_rand($font[$i]['width'] - $this->width / 20, $font[$i]['width'] + $this-> width / 20);
 $font[$i]['height'] = mt_rand($font[$i]['height'] - $this->width / 20, $font[$i]['height'] + $this-> width / 20);
 }
 $y = mt_rand(0, $this->height - $font[$i]['height']);
 if($this->shadow) {
 $this->imcodeshadow = $this->imcode;
 imagecolorset($this->imcodeshadow, 0 , 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);
 imagecopyresized($this->im, $this->imcodeshadow, $x + 1, $y + 1, 0, 0, $font[$i]['width'], $font[$i]['height' ], $font[$i]['data'][0], $font[$i]['data'][1]);
 }
 imagecolorset($this->imcode, 0 , $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
 imagecopyresized($this->im, $this->imcode, $x, $y, 0, 0, $font[$i]['width'], $font[$i]['height'], $font [$i]['data'][0], $font[$i]['data'][1]);
 } else {
 $y = mt_rand(0, $this->height - 20);
 if($this->shadow) {
 $text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);
 imagechar($this->im, 5, $x + 1, $y + 1, $seccode[$i], $text_shadowcolor);
 }
 $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
 imagechar($this->im, 5, $x, $y, $seccode[$i], $text_color);
 }
 $x += $font[$i]['width'];
 }
 }
 修改為: 
         function giffont() {複製代碼global $seccodelength;
 $seccode = $this->code;
 $seccodelength = $seccodelength - 1;
 $seccodedir = array();
 if(function_exists('imagecreatefromgif')) {
 $seccoderoot = $this->datapath.'gif/';
 $dirs = opendir($seccoderoot);
 while($dir = readdir($dirs)) {
 if($dir != '.' && $dir != '..' && file_exists($seccoderoot.$dir.'/9.gif')) {
 $seccodedir[] = $dir;
 }
 }
 }
 $widthtotal = 0;
 for($i = 0; $i <= $seccodelength; $i++) {
 $this->imcodefile = $seccodedir ? $seccoderoot.$seccodedir[array_rand($seccodedir)].'/'.strtolower($seccode[$i]).'.gif' : '';
 if(!empty($this->imcodefile) && file_exists($this->imcodefile)) {
 $font[$i]['file'] = $this->imcodefile;
 $font[$i]['data'] = getimagesize($this->imcodefile);
 $font[$i]['width'] = $font[$i]['data'][0] + mt_rand(0, 6) - 4;
 $font[$i]['height'] = $font[$i]['data'][1] + mt_rand(0, 6) - 4;
 $font[$i]['width'] += mt_rand(0, $this->width / 5 - $font[$i]['width']);
 $widthtotal += $font[$i]['width'];
 } else {
 $font[$i]['file'] = '';
 $font[$i]['width'] = 8 + mt_rand(0, $this->width / 5 - 5);
 $widthtotal += $font[$i]['width'];
 }
 }
 $x = mt_rand(1, $this->width - $widthtotal);
 for($i = 0; $i <= $seccodelength; $i++) {
 $this->color && $this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
 if($font[$i]['file']) {
 $this->imcode = imagecreatefromgif($font[$i]['file']);
 if($this->size) {
 $font[$i]['width'] = mt_rand($font[$i]['width'] - $this->width / 20, $font[$i]['width'] + $this-> width / 20);
 $font[$i]['height'] = mt_rand($font[$i]['height'] - $this->width / 20, $font[$i]['height'] + $this-> width / 20);
 }
 $y = mt_rand(0, $this->height - $font[$i]['height']);
 if($this->shadow) {
 $this->imcodeshadow = $this->imcode;
 imagecolorset($this->imcodeshadow, 0 , 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);
 imagecopyresized($this->im, $this->imcodeshadow, $x + 1, $y + 1, 0, 0, $font[$i]['width'], $font[$i]['height' ], $font[$i]['data'][0], $font[$i]['data'][1]);
 }
 imagecolorset($this->imcode, 0 , $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
 imagecopyresized($this->im, $this->imcode, $x, $y, 0, 0, $font[$i]['width'], $font[$i]['height'], $font [$i]['data'][0], $font[$i]['data'][1]);
 } else {
 $y = mt_rand(0, $this->height - 20);
 if($this->shadow) {
 $text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);
 imagechar($this->im, 5, $x + 1, $y + 1, $seccode[$i], $text_shadowcolor);
 }
 $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
 imagechar($this->im, 5, $x, $y, $seccode[$i], $text_color);
 }
 $x += $font[$i]['width'];
 }
 }
 4.打開include\global.func.php  找到: 
 function seccodeconvert(&$seccode) {複製代碼global $seccodedata, $charset;
 $seccode = substr($seccode, -6);
 if($seccodedata['type'] == 1) {
 include_once language('seccode');
 $len = strtoupper($charset) == 'GBK' ? 2 : 3;
 $code = array(substr($seccode, 0, 3), substr($seccode, 3, 3));
 $seccode = '';
 for($i = 0; $i < 2; $i++) {
 $seccode .= substr($lang['chn'], $code[$i] * $len, $len);
 }
 return;
 } elseif($seccodedata['type'] == 3) {
 $s = sprintf('%04s', base_convert($seccode, 10, 20));
 $seccodeunits = 'CEFHKLMNOPQRSTUVWXYZ';
 } else {
 $s = sprintf('%04s', base_convert($seccode, 10, 24));
 $seccodeunits = 'BCEFGHJKMPQRTVWXY2346789';
 }
 $seccode = '';
 for($i = 0; $i < 4; $i++) {
 $unit = ord($s{$i});
 $seccode .= ($unit >= 0x30 && $unit <= 0x39) ? $seccodeunits[$unit - 0x30] : $seccodeunits[$unit - 0x57];
 }
 }
 修改為: 
 function seccodeconvert(&$seccode) {複製代碼global $seccodedata, $charset,$seccodelength;
 
 if($seccodedata['type'] == 1) {
 include_once language('seccode');
 $len = strtoupper($charset) == 'GBK' ? 2 : 3;
 $code = array(substr($seccode, 0, 3), substr($seccode, 3, 3));
 $seccode = '';
 for($i = 0; $i < 2; $i++) {
 $seccode .= substr($lang['chn'], $code[$i] * $len, $len);
 }
 return;
 } elseif($seccodedata['type'] == 3) {
 $s = sprintf('%04s', base_convert($seccode, 10, 20));
 $seccodeunits = 'CEFHKLMNOPQRSTUVWXYZ';
 } else {
 $s = sprintf('%04s', base_convert($seccode, 10, 24));
 $seccodeunits = 'BCEFGHJKMPQRTVWXY2346789';
 }
 $seccode = '';
 for($i = 0; $i < $seccodelength; $i++) {
 $unit = ord($s{$i});
 $seccode .= ($unit >= 0x30 && $unit <= 0x39) ? $seccodeunits[$unit - 0x30] : $seccodeunits[$unit - 0x57];
 }
 }
 5.打開include\common.inc.php  找到: 
 if(!$sessionexists) {複製代碼if($discuz_uid) {
 if(!($_DSESSION = $db->fetch_first("SELECT $membertablefields, m.styleid
 FROM {$tablepre}members m WHERE m.uid='$discuz_uid' AND m.password='$discuz_pw' AND m.secques='$discuz_secques'"))) {
 clearcookies();
 }
 }
 
 if(ipbanned($onlineip)) $_DSESSION['ipbanned'] = 1;
 
 $_DSESSION['sid'] = random(6);
 $_DSESSION['seccode'] = random(6, 1);
 }
 在之後增加: 
 $seccodelength = $_DCOOKIE['secc'];複製代碼$seccodelength = preg_match('/(\d)/im',$seccodelength,$match);
 if($seccodelength == 1) {
 $seccodelength = $match[1];
 } else {
 $seccodelength = 4;
 }
 
 if($seclevel) {
 $seccodelength = 5;
 }
 
 if($seccodelength > 7) {
 $seccodelength = 7;
 } elseif($seccodelength < 2) {
 $seccodelength = 2;
 }
 [此文章售價 500 雅幣已有 1 人購買] 
若發現會員採用欺騙的方法獲取財富,請立刻舉報,我們會對會員處以2-N倍的罰金,嚴重者封掉ID!
 
 
 [ 此文章被jackli000在2012-08-15 14:37重新編輯 ] 
 
 |