Manyou诚邀开发者加盟 | 常见问题
《站长》俱乐部官方群公布 城市联络员机会多多
立即免费下载 Discuz!7.0.0 正式版
安装升级指南 | 用户手册 | 新手建站学堂
基于.net架构的Discuz!NT2.6正式版发布
Comsenz公司2009年招贤纳士,期待您的加盟
Discuz!收费服务内容及价格
7月重庆、成都、西安三地社区运营实战特训速报名
Discuz!/UCHome 专用官方虚拟主机
牧场新服一起联运:豆浆机免费带回家
基于Discuz!的免费论坛空间5D6D
康盛创想《站长》俱乐部18城市互动之旅报名ing
返回列表 回复 发帖

[Hack]随机长度的数字验证码(插件练习,欢迎讨论)

这里有中文验证码:
http://www.discuz.net/thread-481189-1-1.html


*********************
[Hack]随机长度的数字验证码
功能描述:每次刷新都会产生2~8位的数字验证码!
作者:badfox
指导:习明
备注:具体有什么用,还没想到,仅为学习!
*********************
---全新安装开始:
一、升级数据库:
  1. ALTER TABLE `cdb_sessions` CHANGE `seccode` `seccode` bigint(12) UNSIGNED NOT NULL DEFAULT '0' ;
复制代码
二、打开global.func.php
查找:
  1. if(!defined('IN_DISCUZ')) {
  2.         exit('Access Denied');
  3. }
复制代码
下面添加:
  1. function seccodelen() {
  2.        
  3.          mt_srand((double)microtime() * 1000000);
  4.         $seccodelen = mt_rand(2,8);
  5.         return $seccodelen;
  6.         }
复制代码
查找:(有两处
  1.         $seccode = random(4, 1);
复制代码
替换为:
  1.         $seccode = random(seccodelen(), 1);
复制代码
三、打开 seccode.php
全选,全部替换为:
  1. <?php

  2. /*
  3.         [Discuz!] (C)2001-2006 Comsenz Inc.
  4.         This is NOT a freeware, use is subject to license terms

  5.         $RCSfile: seccode.php,v $
  6.         $Revision: 1.11.2.1 $
  7.         $Date: 2006/12/12 $
  8. */

  9. define('CURSCRIPT', 'seccode');

  10. require_once './include/common.inc.php';

  11. if(preg_replace("/https?:\/\/([^\:\/]+).*/i", "\\1", $_SERVER['HTTP_REFERER']) != preg_replace("/([^\:]+).*/", "\\1", $_SERVER['HTTP_HOST'])) {
  12.         exit('Access Denied');
  13. }

  14. if($update) {
  15.         $seccode = random(seccodelen(), 1);
  16.         updatesession();
  17. }

  18. if($seccode < 1 || $seccode > 99999999) {
  19.         exit('Access Denied');
  20. }

  21. $seclen = strlen($seccode);
  22. $seccode = sprintf('%02d', $seccode);
  23. $image_x = $seclen*15;
  24. $image_y = 25;      


  25. if(!$nocacheheaders) {
  26.         @header("Expires: -1");
  27.         @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
  28.         @header("Pragma: no-cache");
  29. }

  30. if(function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') && function_exists('imagecolorallocate') && function_exists('imagesetpixel') && function_exists('imagechar') && function_exists('imagecreatefromgif') && function_exists('imagepng'))
  31.         {

  32.         $im = imagecreate($image_x, $image_y);
  33.         $backgroundcolor = imagecolorallocate ($im, 255, 255, 255);
  34.         $numorder = range(1, $seclen);
  35.         shuffle($numorder);
  36.         $numorder = array_flip($numorder);

  37.         for($i = 1; $i <= $seclen; $i++) {
  38.                 $imcodefile = 'images/common/number'.$seccode[$numorder[$i]].'.gif';
  39.                 $x = $numorder[$i] * 13 + mt_rand(0, 4) - 2;
  40.                 $y = mt_rand(0, 3);
  41.                 if(file_exists($imcodefile)) {
  42.                         $imcode = imagecreatefromgif($imcodefile);
  43.                         $data = getimagesize($imcodefile);
  44.                         imagecolorset($imcode, 0 ,mt_rand(50, 255), mt_rand(50, 128), mt_rand(50, 255));
  45.                         imagecopyresized($im, $imcode, $x, $y, 0, 0, $data[0] + mt_rand(0, 6) - 3, $data[1] + mt_rand(0, 6) - 3, $data[0], $data[1]);
  46.                 } else {
  47.                         $text_color = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 128), mt_rand(50, 255));
  48.                         imagechar($im, 5, $x + 5, $y + 3, $seccode[$numorder[$i]], $text_color);
  49.                 }
  50.         }


  51.         $linenums = mt_rand(10, 32);
  52.         for($i=0; $i <= $linenums; $i++) {
  53.                 $linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  54.                 $linex = mt_rand(0, $image_x);
  55.                 $liney = mt_rand(0, $image_y);
  56.                 imageline($im, $linex, $liney, $linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor);
  57.         }

  58.         for($i=0; $i <= 64; $i++) {
  59.                 $pointcolor = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 255), mt_rand(50, 255));
  60.                 imagesetpixel($im, mt_rand(0, $image_x), mt_rand(0, $image_y), $pointcolor);
  61.         }

  62.         $bordercolor = imagecolorallocate($im , 150, 150, 150);
  63.         imagerectangle($im, 0, 0, $image_x-1, $image_y-1, $bordercolor);


  64.         header('Content-type: image/png');
  65.         imagepng($im);
  66.         imagedestroy($im);

  67. } else {

  68.         $numbers = array
  69.                 (
  70.                 0 => array('3c','66','66','66','66','66','66','66','66','3c'),
  71.                 1 => array('1c','0c','0c','0c','0c','0c','0c','0c','1c','0c'),
  72.                 2 => array('7e','60','60','30','18','0c','06','06','66','3c'),
  73.                 3 => array('3c','66','06','06','06','1c','06','06','66','3c'),
  74.                 4 => array('1e','0c','7e','4c','2c','2c','1c','1c','0c','0c'),
  75.                 5 => array('3c','66','06','06','06','7c','60','60','60','7e'),
  76.                 6 => array('3c','66','66','66','66','7c','60','60','30','1c'),
  77.                 7 => array('30','30','18','18','0c','0c','06','06','66','7e'),
  78.                 8 => array('3c','66','66','66','66','3c','66','66','66','3c'),
  79.                 9 => array('38','0c','06','06','3e','66','66','66','66','3c')
  80.                 );

  81.         for($i = 0; $i < 10; $i++) {
  82.                 for($j = 0; $j < 8; $j++) {
  83.                         $a1 = substr('012', mt_rand(0, 2), 1).substr('012345', mt_rand(0, 5), 1);
  84.                         $a2 = substr('012345', mt_rand(0, 5), 1).substr('0123', mt_rand(0, 3), 1);
  85.                         mt_rand(0, 1) == 1 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a1);
  86.                         mt_rand(0, 1) == 0 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a2);
  87.                 }
  88.         }

  89. $slen = $seclen > 4 ? $slen = 8 : $slen = 4;


  90.         $bitmap = array();
  91.         for($i = 0; $i < 20; $i++) {
  92.                 for($j = 0; $j < $slen; $j++) {
  93.                         $n = substr($seccode, $j, 1);
  94.                         $bytes = $numbers[$n][$i];
  95.                         $a = mt_rand(0, 14);
  96.                         switch($a) {
  97.                                 case 1: str_replace('9', '8', $bytes); break;
  98.                                 case 3: str_replace('c', 'e', $bytes); break;
  99.                                 case 6: str_replace('3', 'b', $bytes); break;
  100.                                 case 8: str_replace('8', '9', $bytes); break;
  101.                                 case 0: str_replace('e', 'f', $bytes); break;
  102.                         }
  103.                         array_push($bitmap, $bytes);
  104.                 }
  105.         }


  106.         for($i = 0; $i < 8; $i++) {
  107.                 $a = substr('012', mt_rand(0, 2), 1) . substr('012345', mt_rand(0, 5), 1);
  108.                 array_unshift($bitmap, $a);
  109.                 array_push($bitmap, $a);
  110.         }
  111. $imx = dechex($seclen*8);
  112.         $image = pack('H*', '424d9e000000000000003e00000028000000'.$imx.'00000018000000010001000000'.
  113.                         '0000600000000000000000000000000000000000000000000000FFFFFF00'.implode('', $bitmap));

  114.         header('Content-Type: image/bmp');
  115.         echo $image;
  116. }

  117. ?>
复制代码
四、打开register.php

查找:
  1.         $seccode = random(4, 1);
复制代码
替换为:
  1.         $seccode = random(seccodelen(), 1);
复制代码
五、打开common.inc.php

查找:
  1. $_DSESSION['seccode'] = random(4, 1);
复制代码
替换为:
  1. $_DSESSION['seccode'] = random(seccodelen(), 1);
复制代码
六、打开register.htm
查找:
  1.         <input type="text" name="seccodeverify" size="4" maxlength="4" id="seccodeverify" onBlur="checkseccode()">
复制代码
替换为:
  1. <input type="text" name="seccodeverify" size="8" maxlength="8" id="seccodeverify" onBlur="checkseccode()">
复制代码
查找:
  1. var profile_seccode_invalid = '{lang register_profile_seccode_invalid}';
复制代码
替换为:
  1.         var profile_seccode_invalid = '验证码为阿拉伯数字,请正确填写。如看不清图片中的文字请点击图片刷新验证码。';
复制代码
查找:
  1. if(!(/[0-9]{4}/.test(seccodeverify))) {
复制代码
替换为:
  1.         if(!(/^[0-9]*$/.test(seccodeverify))) {
复制代码
---全新安装结束!

[ 本帖最后由 badfox 于 2006-12-12 17:46 编辑 ]

效果图

1.jpg
1

评分次数

Discuz! Support Team Forum:   Discuz!系列程序的独立第三方团队
论坛注册:http://www.discuzsupport.net/register.php?fromuid=139
(我的宣传地址,给加点宣传度吧,拜托!)
*********************
[Hack]随机长度的数字验证码
功能描述:每次刷新都会产生2~9位的数字验证码!
作者:badfox
指导:习明
备注:
跟1楼的作用一样,只是在服务器不支持GD库函数情况下,生成更为美观的验证码!
缺点是服务器不支持GD库函数情况下所生成的美观验证码,不能点击刷新!(还没想到解决办法,欢迎讨论!)

**********************
---全新安装开始:
一、升级数据库:
  1. ALTER TABLE `cdb_sessions` CHANGE `seccode` `seccode` bigint(12) UNSIGNED NOT NULL DEFAULT '0' ;
复制代码
二、打开global.func.php
查找:
  1. if(!defined('IN_DISCUZ')) {
  2.         exit('Access Denied');
  3. }
复制代码
下面添加:
  1. function seccodelen() {
  2.        
  3.          mt_srand((double)microtime() * 1000000);
  4.         $seccodelen = mt_rand(2,9);
  5.         return $seccodelen;
  6.         }
复制代码
查找:(有两处
  1. $seccode = random(4, 1);
复制代码
替换为:
  1. $seccode = random(seccodelen(), 1);
复制代码
三、打开 seccode.php
全选,全部替换为:
  1. <?php

  2. /*
  3.         [Discuz!] (C)2001-2006 Comsenz Inc.
  4.         This is NOT a freeware, use is subject to license terms

  5.         $RCSfile: seccode.php,v $
  6.         $Revision: 1.11.2.1 $
  7.         $Date: 2006/12/12 $
  8. */

  9. define('CURSCRIPT', 'seccode');

  10. require_once './include/common.inc.php';

  11. if(preg_replace("/https?:\/\/([^\:\/]+).*/i", "\\1", $_SERVER['HTTP_REFERER']) != preg_replace("/([^\:]+).*/", "\\1", $_SERVER['HTTP_HOST'])) {
  12.         exit('Access Denied');
  13. }

  14. if($update) {
  15.         $seccode = random(seccodelen(), 1);
  16.         updatesession();
  17. }

  18. if($seccode < 1 || $seccode > 999999999) {
  19.         exit('Access Denied');
  20. }

  21. $seclen = strlen($seccode);
  22. $seccode = sprintf('%02d', $seccode);
  23. $image_x = $seclen*15;
  24. $image_y = 25;      


  25. if(!$nocacheheaders) {
  26.         @header("Expires: -1");
  27.         @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
  28.         @header("Pragma: no-cache");
  29. }

  30. if($gdyn){

  31.         $im = imagecreate($image_x, $image_y);
  32.         $backgroundcolor = imagecolorallocate ($im, 255, 255, 255);
  33.         $numorder = range(1, $seclen);
  34.         shuffle($numorder);
  35.         $numorder = array_flip($numorder);

  36.         for($i = 1; $i <= $seclen; $i++) {
  37.                 $imcodefile = 'images/common/number'.$seccode[$numorder[$i]].'.gif';
  38.                 $x = $numorder[$i] * 13 + mt_rand(0, 4) - 2;
  39.                 $y = mt_rand(0, 3);
  40.                 if(file_exists($imcodefile)) {
  41.                         $imcode = imagecreatefromgif($imcodefile);
  42.                         $data = getimagesize($imcodefile);
  43.                         imagecolorset($imcode, 0 ,mt_rand(50, 255), mt_rand(50, 128), mt_rand(50, 255));
  44.                         imagecopyresized($im, $imcode, $x, $y, 0, 0, $data[0] + mt_rand(0, 6) - 3, $data[1] + mt_rand(0, 6) - 3, $data[0], $data[1]);
  45.                 } else {
  46.                         $text_color = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 128), mt_rand(50, 255));
  47.                         imagechar($im, 5, $x + 5, $y + 3, $seccode[$numorder[$i]], $text_color);
  48.                 }
  49.         }


  50.         $linenums = mt_rand(10, 32);
  51.         for($i=0; $i <= $linenums; $i++) {
  52.                 $linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  53.                 $linex = mt_rand(0, $image_x);
  54.                 $liney = mt_rand(0, $image_y);
  55.                 imageline($im, $linex, $liney, $linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor);
  56.         }

  57.         for($i=0; $i <= 64; $i++) {
  58.                 $pointcolor = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 255), mt_rand(50, 255));
  59.                 imagesetpixel($im, mt_rand(0, $image_x), mt_rand(0, $image_y), $pointcolor);
  60.         }

  61.         $bordercolor = imagecolorallocate($im , 150, 150, 150);
  62.         imagerectangle($im, 0, 0, $image_x-1, $image_y-1, $bordercolor);


  63.         header('Content-type: image/png');
  64.         imagepng($im);
  65.         imagedestroy($im);

  66. } else {
  67. /****************
  68. 这里原先的代码是在系统不支持GD库情况下由Xbm原理生成验证码图像,由于我根本不了解PHP生成Xbm的原理,所以找来了
  69. [太阳雨@喜悦国际村]
  70. 做的一个由表格生成验证码的程序,十分另类,好像也很有效,生成的验证码是由表格组成的!
  71. 要想强制看效果的话,把common.inc.php最后面加的那个if()判断后面加上
  72. $gdyn=0;
  73. *****************/
  74. $num = array(
  75. "0"=>array(12,13,14,15,16,22,26,32,36,42,46,52,56,62,66,72,73,74,75,76),
  76. "1"=>array(12,22,32,42,52,62,72),
  77. "2"=>array(12,13,14,15,16,26,36,42,43,44,45,46,52,62,72,73,74,75,76),
  78. "3"=>array(12,13,14,15,16,26,36,42,43,44,45,46,56,66,72,73,74,75,76),
  79. "4"=>array(12,16,22,26,32,36,42,43,44,45,46,56,66,76),
  80. "5"=>array(12,13,14,15,16,22,32,42,43,44,45,46,56,66,72,73,74,75,76),
  81. "6"=>array(12,13,14,15,16,22,32,42,43,44,45,46,52,56,62,66,72,73,74,75,76),
  82. "7"=>array(12,13,14,15,16,26,36,46,56,66,76),
  83. "8"=>array(12,13,14,15,16,22,26,32,36,42,43,44,45,46,52,56,62,66,72,73,74,75,76),
  84. "9"=>array(12,13,14,15,16,22,26,32,36,42,43,44,45,46,56,66,72,73,74,75,76)
  85. );


  86. echo "document.writeln(\"<table border=0 style='line-height:0.8;letter-spacing:0.8;font-size:4px'><tr>\");";
  87. for($i=0;$i<$seclen;$i++){
  88.     echo "document.writeln(\"<td>\");";
  89.     for($j=0;$j<9;$j++){
  90.         for($k=0;$k<9;$k++){
  91.             $v = $j.$k;
  92.             if(in_array($v,$num[$seccode[$i]])) echo "document.writeln(\"<font style='font-color:#ff0000;'><B>O</B></font>\");";
  93.             else echo "document.writeln(\"<B>&nbsp;&nbsp;&nbsp;</B>\");";
  94.         }
  95.         echo "document.writeln(\"<br>\");";
  96.     }
  97.     echo "document.writeln(\"</td>\");";
  98. }
  99. echo "document.writeln(\"</tr></table>\");";

  100. }

  101. ?>
复制代码
四、打开register.php

查找:
  1. $seccode = random(4, 1);
复制代码
替换为:
  1. $seccode = random(seccodelen(), 1);
复制代码
五、打开common.inc.php查找
  1. ?>
复制代码
上面添加
  1. if(function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') && function_exists('imagecolorallocate') && function_exists('imagesetpixel') && function_exists('imagechar') && function_exists('imagecreatefromgif') && function_exists('imagepng')){
  2. $gdyn = 1;
  3. }else{
  4. $gdyn = 0;
  5. }
复制代码
查找:
  1. $_DSESSION['seccode'] = random(4, 1);
复制代码
替换为:
  1. $_DSESSION['seccode'] = random(seccodelen(), 1);
复制代码
六、打开register.htm

        查找
  1.                 <!--{if $seccodecheck}-->
  2.                 <tr>
  3.                 <td class="altbg1" width="21%"><span class="bold">{lang seccode}</span></td>
  4.                 <td class="altbg2"><div class="input"><input type="text" name="seccodeverify" size="4" maxlength="4" id="seccodeverify" onBlur="checkseccode()"> <img id="seccode" style="cursor:pointer" onclick="this.src='seccode.php?update=' + Math.random()" src="seccode.php" class="absmiddle" alt="" /></div><div class="notice" id="checkseccode"> {lang secode_refresh}</div>
  5.                 <script type="text/javascript">$('seccode').src = 'seccode.php?update=' + Math.random()</script></td>
  6.                 </tr>
  7.         <!--{/if}-->
复制代码
替换为:
  1.         <!--{if $seccodecheck}-->
  2.         <tr><td class="altbg1" width="21%"><span class="bold">{lang seccode}</span></td>
  3.         <td class="altbg2"><div class="input"><div style="float:left"><input type="text" name="seccodeverify" size="9" maxlength="9" id="seccodeverify" onBlur="checkseccode()">&nbsp;</div> <!--{if $gdyn}--><img id="seccode" style="cursor:pointer" onclick="this.src='seccode.php?update=' + Math.random()" src="seccode.php" class="absmiddle" alt="" /><!--{else}--><div style="cursor:pointer;float:left; border: solid;border-width: 1px; "><script language="JavaScript" src="seccode.php"></script></div><!--{/if}--></div><div class="notice" id="checkseccode"> <!--{if $gdyn}-->{lang secode_refresh}<!--{else}-->请输入你看到的数字!<!--{/if}--></div><script type="text/javascript">$('seccode').src = 'seccode.php?update=' + Math.random()</script></td></tr>
  4.         <!--{/if}-->
复制代码
查找:
  1. var profile_seccode_invalid = '{lang register_profile_seccode_invalid}';
复制代码
替换为:
  1.         var profile_seccode_invalid = '验证码为阿拉伯数字,请正确填写。如看不清图片中的文字请点击图片刷新验证码。';
复制代码
查找:
  1. if(!(/[0-9]{4}/.test(seccodeverify))) {
复制代码
替换为:
  1. if(!(/^[0-9]*$/.test(seccodeverify))) {
复制代码
---全新安装结束!

[ 本帖最后由 badfox 于 2006-12-12 16:19 编辑 ]

效果图

2.jpg
Discuz! Support Team Forum:   Discuz!系列程序的独立第三方团队
论坛注册:http://www.discuzsupport.net/register.php?fromuid=139
(我的宣传地址,给加点宣传度吧,拜托!)
:) 来个板凳支持一下辛苦的劳动吧~
免费资源推荐专 家http://www.hotgsu.com
用我的真诚推荐免费资源换你的一个IP.
  
原帖由 binbin85129 于 2006-12-12 16:16 发表
:) 来个板凳支持一下辛苦的劳动吧~
谢谢支持!
Discuz! Support Team Forum:   Discuz!系列程序的独立第三方团队
论坛注册:http://www.discuzsupport.net/register.php?fromuid=139
(我的宣传地址,给加点宣传度吧,拜托!)
真的做了呀 不错哦 嘿嘿
狐狸以后专门反暴力吧
备注:具体有什么用,还没想到,仅为学习!
汗一个
原帖由 习明 于 2006-12-12 16:25 发表
备注:具体有什么用,还没想到,仅为学习!
汗一个

确实没想到嘛!
Discuz! Support Team Forum:   Discuz!系列程序的独立第三方团队
论坛注册:http://www.discuzsupport.net/register.php?fromuid=139
(我的宣传地址,给加点宣传度吧,拜托!)

需要啊~~~~~~~
不错不错,支持一下
稳定高速的主机空间、专业完善的售后服务..双线主机、服务器合租,通用网址,新互域名,咨询QQ:8619910
留名支持!
返回列表