+ -
当前位置:首页 → 问答吧 → 这个登录程序的验证码如何用JS去判断?请各位高手相助

这个登录程序的验证码如何用JS去判断?请各位高手相助

时间:2010-07-12

来源:互联网

<?php
 session_start();
 include("conn.php");
 if($_POST['btnLogin']){
  $user=$_POST['user'];
  $pwd=$_POST['pass'];
  $yzm=$_POST['yzm'];
      
  $sql="select * from `access` where `name`='$user' and `pwd`='$pwd'";
  $rs=mysql_query($sql);
  $result=mysql_fetch_array($rs);
  if($result){
   header("location:list.php");
   $_SESSION[user]=$user;
  }else{
   echo "用户名或密码错误!";
  }
  
 }
?>
<html>
 <head>
  <title>添加留言</title>
 </head>
 <script language="javascript">
 function check(){
  if(document.login.user.value==""){
   alert("用户名不能为空!");
   login.user.focus();
   return false;
  }
  if(document.login.pass.value==""){
   alert("密码不能为空!");
   login.pass.focus();
   return false;
  }
  if(document.login.yzm.value==""){
   alert("验证码不能为空!");
   login.yzm.focus();
   return false;
  }
  if(document.login.yzm.value!=document.login.hide.value){
   alert("验证码错误!");
   login.yzm.focus();
   return false;
  }
 }
 </script>
 
 <body>
 <?php
  include("top.html");
 ?>
 <center>
 <table border="1">
 <form name="login" method="post" action="login.php" onsubmit="return check()">
 <tr>
  <td>昵称:</td>
  <td><input type="text" name="user"></td>
 </tr>
 <tr>
  <td>密码:</td>
  <td><input type="password" name="pass"></td>
 </tr>
 <tr>
  <td>验证码:</td>
  <td><input type="text" size="4" name="yzm"><img src="imagecode.php"></td>
  
  <input type="hidden" name="hide" value="<?php echo $_SESSION['check_pic']?>">
  
 </tr>
 <tr align="center">
 <td colspan="2"><input type="submit" name="btnLogin" value="登录">
 <input type="reset" name="btnCancel" value="取消">   <a href="reg.php">注册</a>
 </td>
 </tr>
 </form>
 </table>
 </center>
 </body>
</html>

说明:其中$_SESSION['check_pic']为imagecode.php 产生的验证码
这个登录程序的验证码如何用JS去判断?请各位高手相助

作者: phua   发布时间: 2010-07-12

document.forms[0].yzm.blur=function(){
           if(this.value.toUpperCase()!=document.forms[0].hide.value.toUpperCase()){
                 alert('输入不正确')
                 return false;
          }
}

作者: ynwcel   发布时间: 2010-07-12

写到js脚本中还是不行,该怎么解决下

作者: phua   发布时间: 2010-07-12

//js中的代码
if(document.login.yzm.value!=document.login.hide.value){
            alert("输入不正确");
            login.yzm.focus();
            return false;
        }
//form中的判断验证码代码
<tr>
        <td>验证码:</td>
        <td><input type="text" size="4" name="yzm"><img src="imagecode.php"></td>
        
        <input type="hidden" name="hide" value="<?php echo $_SESSION['check_pic']?>">
        <?php echo $_SESSION['check_pic']?>
    </tr>
请大侠帮我看一下,到底哪错了?

作者: phua   发布时间: 2010-07-12

你没给<img src="imagecode.php"> 传值 传值过去就好了

作者: ysz9999   发布时间: 2010-07-12

<td><input type="text" size="4" name="yzm"> 这句加value="" 没有value属性  document.login.yzm.value这个就会出错。

作者: 李思   发布时间: 2010-07-12

加一个隐藏域,初始化的时候 他的值为<?php echo $_SESSION['check_pic']?>
然后提交的时候判断
function check(){
if(doucument.form.login.yzm.value != document.form.login.hide.value)
{
   alert('验证码不正确');
return false;
}
}
">
<form name="login" method="post" action="login.php" onsubmit="return check()">

作者: yuejide   发布时间: 2010-07-12

我用<input type="hidden" name="hide" value="<?php echo $_SESSION['check_pic']?>">
这个语句,把imagecode.php中的值传到hide这个框中,然后在通过yzm中的值与hide框中的值进行判断,
我觉得我的思路没错的,我是传值了

作者: phua   发布时间: 2010-07-12

上海九星机构专业*****经济师详情QQ951400296

作者: 改良继   发布时间: 2010-07-12

你把<input type="hidden" name="hide" value="<?php echo $_SESSION['check_pic']?>">
这一句的"hidden"改为“text”,就会看到他的值与验证码的值是不相等的

作者: jetshow   发布时间: 2010-09-06