用PHP+JS+MYSQL实现用户登陆验证,的具体步骤是怎么样的呢

马上就PHP要期末考试,得赶紧练练呢?我自己的想法是,首先用html做一个表单,先用JS验证密码和用户名是否为空,然后PHP获得表单数据和MYSQL的数据库(MYSQL实现创建好了ID和密码)进行对比,相同则返回登录成功,不相同则返回失败;不知道是不是这样的,跪求大神们给分析分析,感激不尽呢!!! 还有和数据库进行比对的时候是怎样操作的呢???

第一次学PHP就是做这个验证..

html做个表单,

当表单onsubmit=return check();调用自写js来判断用户名和密码是否为空,

如果是空就alert不能为空,然后return false;相反则return true;

而接收的PHP也要验证是否为空,如果严谨点还要对提交的数据进行过滤,防止sql注入。

然后php再根据提交的数据搜MYSQL,如果用户名和密码都相同时,echo 登录成功,相反则登录失败.


<html>
<script>
function check(obj){
 with(obj){
     if((user.value+"").length <= 0){
          alert("用户名不能为空");
          return false;
     }else if((pwd.value+"").length <= 0){
     
         alert("用户名不能为空");
         return false;
     }else{
         return true;
     }
 }
}
</script>
<body>
  <form action="check.php" method="post" onsubmit="return check(this)">
    <input type="text" name="user" value="">
    <input type="password" name="pwd" value="">
    <input type="submit" name="submit" value="登录">
    <input type="cancel" name="cancel" value="取消">
  </form>
</body>
</html>


<?php
$conn = mysql_connect( "数据库地址", "数据库用户名", "密码" );
mysql_query("set names utf8");
mysql_select_db( "数据库名" );

function inject_check($sql_str){     
return preg_match("/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|%|eval|=|and|'|;|exec|count/i", $sql_str);    // 进行过滤
}

if(!empty($_POST)){
foreach($_POST as $key => $value){
if(inject_check($value)){
exit ('<script>alert("地址栏输入发现有非法字符,请重新输入!");history.go(-1);</script>');
die ();
}
}
}

$res = mysql_query("SELECT count(*) as m from `表名` where 用户名='${_POST['user']}' AND 密码='${_POST['pwd']}'");

$row = mysql_fetch_object($res);
if($row->m >0){
    echo "登陆成功";
}else{
    echo "用户名或密码错误";
}
exit;
?>

追问

非常感谢呢,终于搞定了
$row = mysql_fetch_object($res);
if($row->m >0)用对象元素做判断感觉有点不怎么实用呢,用更好的方法吗,更易于理解的

追答

mysql_num_row

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-05-31
<form id="form1" action="dosubmit.php" method="post">
<p>username : <input type="text" name="username" id="username"/></p>
<p>password: <input type="text" name="password" id="password"/></p>
<input type="button" value="Submit" onclick="dosubmit"/>
</form>

<script type="text/javascript">
function dosubmit(){
if($("#username").val()){
alert("用户名不能为空");
return;
}

if($("#password").val()){
alert("密码不能为空");
return;
}

$("#form1").submit();

}
</script>

dosubmit.php页面
接收用:
$_POST['username']
$_POST['password']

和数据库中数据比较
第2个回答  2013-05-31
先客户端验证,再服务器端验证。利用sql语句查询用户名和密码是否一致,例如:
select * from users where name='qq' and pwd='123'
第3个回答  2013-05-31

网上下载的,非常简单的,看这操练操练

第4个回答  2013-05-31
ajax可以帮到你。。。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网