免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: nothing9
打印 上一主题 下一主题

怎样把用户登陆表单和验证码结合起来? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2006-03-29 21:30 |只看该作者

再次改写了部分代码,请帮忙指导一下!

初学php,写的很乱,以下代码可以验证单一用户,请问:如何加入从mysql数据表中读取数据来验证多个用户?

<?php
//Filename:authpage.php
srand((double)microtime()*1000000);

$array="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

for($i=0;$i<4;$i++)
  {
  $authnum .=substr($array,rand(0,61),1);
  }
?>
<?
if ((!isset($_POST['user'])) || (!isset($_POST['pass'])) || (!isset($_POST['authinput']))) {
?>
<form action="<?php echo $PHP_SELF; ?>" method="post">
    用户名:<input type="text" name="user" value="abcd" style="width: 80px" />
    密码:<input type="text" name="pass" value="1234" style="width: 80px" />
    验证码:<input type="text" name="authinput" style="width: 80px" />
    <input type="hidden" name="authnum" value="<? echo $authnum; ?>" />
    <img src="authimg.php?authnum=<? echo $authnum; ?>" />
    <input type="submit" name="验证" value="登陆" />
</form>
<?
exit;

} else if ((isset($_POST['user'])) && (isset($_POST['pass'])) || (!isset($_POST['authinput']))){

/* Values contain some values, so check to see if they're correct */

if (($_POST['user'] != "abcd") || ($_POST['pass'] != "1234") || strcmp($_POST['authnum'],$_POST['authinput']) != "0") {
?>
<form action="<?php echo $PHP_SELF; ?>" method="post">
    用户名:<input type="text" name="user" style="width: 80px" />
    密码:<input type="password" name="pass" style="width: 80px" />
    验证码:<input type="text" name="authinput" style="width: 80px" />
    <input type="hidden" name="authnum" value="<? echo $authnum; ?>" />
    <img src="authimg.php?authnum=<? echo $authnum; ?>" />
    <input type="submit" name="验证" value="登陆" />
</form>
<?
exit;
} else if (($_POST['user'] == "abcd") || ($_POST['pass'] == "1234") || (strcmp($_POST['authnum'],$_POST['authinput'])== "0" )) {
/* if all values are correct, print success message */
echo "<P>You're authorized!</p>";
}
}
?>
<?php
include "footer.php";
?>


数据库
$myconn=mysql_connect("localhost","root","abcd1234");
mysql_select_db("mydb",$myconn);
$strSql="select * from user where user='".$_POST['user']."' and passwd='".$_POST['pass']."'";

如何通过mysql数据库来验证,而不是用 if (($_POST['user'] == "abcd") || ($_POST['pass'] == "1234")) 来判断?

论坛徽章:
0
12 [报告]
发表于 2006-04-01 22:55 |只看该作者

请高手帮忙指点改正一下以下代码的错误之处

Windows XP SP2 + Apache 1.3.33 + MySQL 4.0.21 + PHP 4.3.10

authnum.php
  1. <?
  2. Header("Content-type:image/png");
  3. session_start();
  4. $authnum_session = '';
  5. $str = 'abcdefghijkmnpqrstuvwxyz1234567890';
  6. $l = strlen($str);
  7. for($i=1;$i<=4;$i++)
  8. {
  9. $num=rand(0,$l-1);
  10. $authnum_session.= $str[$num];
  11. }
  12. $_SESSION['authnum_session'] = $authnum_session;
  13. srand((double)microtime()*1000000);
  14. $im = imagecreate(50,20);
  15. $black = ImageColorAllocate($im, 0,0,0);
  16. $white = ImageColorAllocate($im, 255,255,255);
  17. $gray = ImageColorAllocate($im, 200,200,200);
  18. imagefill($im,68,30,$gray);
  19. $li = ImageColorAllocate($im, 220,220,220);
  20. for($i=0;$i<3;$i++)
  21. {
  22. imageline($im,rand(0,30),rand(0,21),rand(20,40),rand(0,21),$li);
  23. }
  24. imagestring($im, 5, 8, 2, $authnum_session, $white);
  25. for($i=0;$i<90;$i++)
  26. {
  27. imagesetpixel($im, rand()%70 , rand()%30 , $gray);
  28. }
  29. ImagePNG($im);
  30. ImageDestroy($im);
  31. ?>
复制代码

login.php

  1. <?php
  2. /*
  3. --------------------------------------------------------
  4. CREATE TABLE user (
  5.    id smallint(6) NOT NULL auto_increment,
  6.    user varchar(12) NOT NULL,
  7.    passwd varchar(32) NOT NULL,
  8.    UNIQUE id (id)
  9. );
  10. --------------------------------------------------------
  11. */
  12. session_start();
  13. if ($_POST['user']=="" && $_POST['pass']=="" && !isset($HTTP_POST_VARS['authinput']))
  14. {
  15. ?>
  16. <form action="<?php echo $PHP_SELF ?>" method="post">
  17. user:<input type="text" name="user" value="abcd"><br>
  18. pass:<input type="text" name="pass" value="1234"><br>
  19. auth:<input type="text" name="authinput">
  20. <input type="text" name="authnum" style="width: 80px" value="<? echo $_SESSION['authnum_session']; ?>" />
  21. <img src="authnum.php?authnum=<? echo $_SESSION['authnum_session']; ?>">
  22. <input type="submit" value="ok">
  23. </form>
  24. <?
  25. }
  26. else
  27. {
  28.   $myconn=mysql_connect("localhost","root","abcd1234");
  29.   mysql_select_db("mydb",$myconn);
  30.   $strSql="select * from user where user='".$_POST['user']."' and passwd='".md5($_POST['pass'])."'";
  31.   $result=mysql_query($strSql,$myconn);
  32.   $row = mysql_fetch_array($result);
  33.   $id = $row[id];
  34.   mysql_close($myconn);
  35. if($id="" || strcmp($HTTP_POST_VARS['authnum'],$HTTP_POST_VARS['authinput'])!==0)
  36. {
  37. echo "Login Fail!";
  38. }
  39. else
  40. {
  41. echo "OK!<br><a href=\"next.php\">next page</a><br>";
  42. }
  43. }
  44. ?>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP