免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1854 | 回复: 0
打印 上一主题 下一主题

一个登陆验证码的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-11 22:10 |只看该作者 |倒序浏览
有必要先说下目录结构。网站是放在虚拟主机上的。登陆页面在../admincp/login.php,验证码在../inc/rndcode.php
     login.php代码如下:

  1. <?php
  2. require(dirname(__FILE__)."/../inc/config_inc.php");
  3. require(dirname(__FILE__)."/../inc/config_mysql.php");
  4. require(dirname(__FILE__)."/../inc/config_function.php");
  5. $conn = new MysqlConn();
  6. $conn->createcon();
  7. $web_config = $conn->fetch_array("SELECT * FROM `site_config`");
  8. $dopost = $_REQUEST['dopost'];
  9. $username = addslashes($_REQUEST['username']);
  10. $password = substr(md5($_REQUEST['password']),0,23);
  11. $date = date('Y-m-d H:i:s');
  12. $ip = GetIp();
  13. $code = $_POST['code'];
  14. if(empty($dopost)) $dopost="";
  15. if ($dopost == "login") {
  16.     if ($code!==$_SESSION['ecode']||$code=="") {
  17.         ShowMsg("验证码不正确","login.php");
  18.         exit;
  19.     }
  20.     $row = $conn->fetch_array("select * from `admin` where `username`='$username'");
  21.         if (!$row[0] == "") {
  22.             $row2 = $conn->fetch_array("select * from `admin` where `password`='$password'");
  23.             if (!$row2[0] == "") {
  24.                 if ($row2['estate']=="0") {
  25.                     showmsg("你的账户已经被锁定,请与管理员联系",-1);
  26.                     exit;
  27.                 }
  28.                 $_SESSION['username'] = $username;
  29.                 $_SESSION['time'] = time();
  30.                 $count = $row2['count']+1;
  31.                 $uplog = $conn->query("insert into `log`(`username`,`logintime`,`ip`) values('$username','$date','$ip')") or die(mysql_error());
  32.                 $upuser = $conn->query("update `admin` set `count`='$count',`ip`='$ip',`lasttime`='$date' where `username`='$username'")or die(mysql_error());
  33.                 showmsg("欢迎".$username."进入后台","index.php");
  34.                 exit;
  35.             }
  36.             else{
  37.                 showmsg("密码错误!",-1);
  38.                 exit;
  39.             }
  40.         }
  41.         else{
  42.             showmsg("用户名错误!",-1);
  43.             exit;
  44.         }
  45. }
  46. ?>
  47. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  48. <html xmlns="http://www.w3.org/1999/xhtml">
  49. <head>
  50. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  51. <title><?php echo $web_config['webname'].'系统管理登陆';?></title>
  52. <link type="text/css" href="../css/style.css" rel="stylesheet" />
  53. <style type="text/css">
  54. <!--
  55. body {
  56.     background-color: #F1F2F4;
  57. }
  58. -->
  59. </style></head>
  60. <body>
  61. <table width="500"  border="0" align="center" cellpadding="0" cellspacing="0">
  62.   <tr>
  63.     <td width="18" height="16" align="right" valign="bottom"><img src="images/left_top.gif" width="18" height="16" /></td>
  64.     <td align="center" valign="bottom" background="images/row_top.gif"></td>
  65.     <td width="14" align="left" valign="bottom"><img src="images/right_top.gif" width="14" height="16" /></td>
  66.   </tr>
  67.   <tr>
  68.     <td align="right" background="images/left_bg.gif">&nbsp;</td>
  69.     <td align="center" valign="middle" background="images/main_bg.gif"><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="lrd">
  70.       <tr>
  71.         <td height="22" align="center" background="images/admin_tablebar.gif" class="def_text">[ + &nbsp;管理员登陆&nbsp;+ ]</td>
  72.       </tr>
  73.       <tr>
  74.         <td align="center" bgcolor="#CCCCCC"><table width="100%"  border="0" cellspacing="1" cellpadding="0">
  75.           <form action="" method="post">
  76.           <input type="hidden" name="dopost" value="login">
  77.             <tr bgcolor="#F1F2F4">
  78.               <td width="20%" height="25" align="center" class="def_text">管理员用户</td>
  79.               <td height="25" align="center"><input name="username" type="text" class="input" id="username" size="50" /></td>
  80.             </tr>
  81.             <tr bgcolor="#F1F2F4">
  82.               <td width="20%" height="25" align="center" class="def_text">管理员密码</td>
  83.               <td height="12" align="center"><input name="password" type="password" class="input" id="password" size="50" /></td>
  84.             </tr>
  85.             <tr bgcolor="#F1F2F4">
  86.               <td height="12" align="center" class="def_text">验&nbsp; 证&nbsp; 码</td>
  87.               <td height="12" align="center"><table width="75%" border="0" align="center" cellpadding="0" cellspacing="0">
  88.                 <tr>
  89.                   <td width="11%"><div align="left">
  90.                     <input name="code" type="text" class="input" id="code" size="4" maxlength="4" />
  91.                   </div></td>
  92.                   <td width="15%">&nbsp;</td>
  93.                   <td width="74%"><div align="left"><img src="../inc/rndcode.php" /></div></td>
  94.                 </tr>
  95.               </table></td>
  96.               </tr>
  97.             <tr bgcolor="#F1F2F4">
  98.               <td height="30" colspan="2" align="center" bgcolor="#F1F2F4"><input name="goto" type="hidden" id="goto" value="<?php echo $backurl;?>" />
  99.                       <input name="submit_login" type="submit" class="input" id="submit_login" value=" 管 理 员 登 陆 " />
  100.                 <input name="loginyes" type="hidden" id="loginyes" value="1" /></td>
  101.             </tr>
  102.           </form>
  103.         </table></td>
  104.       </tr>
  105.     </table></td>
  106.     <td align="left" background="images/right_bg.gif">&nbsp;</td>
  107.   </tr>
  108.   <tr>
  109.     <td align="right" valign="top"><img src="images/left_down.gif" width="18" height="18" /></td>
  110.     <td align="center" valign="top" background="images/row_down.gif">&nbsp;</td>
  111.     <td align="left" valign="top"><img src="images/right_down.gif" width="14" height="18" /></td>
  112.   </tr>
  113. </table>
  114. <br />
  115. <table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
  116.   <tr>
  117.     <td height="25" align="center" class="def_text"><?php echo $web_config['copyright']?></td>
  118.   </tr>
  119. </table>
  120. </body>
  121. </html>
复制代码


rndcode.php代码如下:

  1. <?
  2. //生成新的四位整数验证码
  3. session_start();//开始会话
  4. $authnum = '';
  5. $str = 'abcdefghijkmnpqrstuvwxyz1234567890';
  6. $l = strlen($str);
  7. for($i=1;$i<=4;$i++)
  8. {
  9. $num=rand(0,$l);
  10. $authnum.= $str[$num];
  11. }
  12. //生成SESSION变量
  13. $_SESSION['ecode']=$authnum;
  14. //生成验证码图片
  15. Header("Content-type: image/PNG");
  16. srand((double)microtime()*1000000);
  17. $im = imagecreate(50,20);
  18. $black = ImageColorAllocate($im, 243,243,243);
  19. $white = ImageColorAllocate($im, 0,0,0);
  20. $gray = ImageColorAllocate($im, 200,200,200);
  21. imagefill($im,68,30,$gray);
  22. //将四位整数验证码绘入图片
  23. imagestring($im, 5, 6, 3, $authnum, $white);
  24. for($i=0;$i<90;$i++) //加入干扰象素
  25. {
  26. imagesetpixel($im, rand()%70 , rand()%30 , $gray);
  27. }
  28. ImagePNG($im);
  29. ImageDestroy($im);
  30. ?>
复制代码


用GB或者遨游下运行login.php能顺利登陆。在IE下就不能登陆,提示验证码错误。后经过测试发现在IE下不能获取到$_SESSION['ecode']的值。这个是什么原因。把rndcode.php移动到admincp目录下就好了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP