使用javascript验证密码
下面是username.php的代码使用javascript验证密码,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,下面是username.php的代码 <html> <head> <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> <!-- <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script> --&
<html>
<head>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<!-- <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script> -->
<script type="text/javascript">
$(document).ready(function(){
$("#username").change(function(){
$("#message").html("<img src='ajax-loader.gif' /> checking...");
var username=$("#username").val();
$.ajax({
type:"post",
url:"check.php",
data:"username="+username,
success:function(data){
if(data==0){
$("#message").html("Username available");
}
else{
$("#message").html("Username already taken");
}
}
});
});
});
</script>
</head>
<body>
<table>
<tr>
<td>Username</td>
<td>:</td>
<td><input type="text" name="id" id="username""/><td>
<td id="message"><td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="text" name="password" id="password" /><td>
</tr>
</table>
</body>
</html>
$(文档).ready(函数(){
$(“#用户名”).change(函数(){
$(“#消息”).html(“检查…”);
var username=$(“#username”).val();
$.ajax({
类型:“post”,
url:“check.php”,
数据:“用户名=”+用户名,
成功:功能(数据){
如果(数据==0){
$(“#消息”).html(“可用用户名”);
}
否则{
$(“#message”).html(“用户名已被使用”);
}
}
});
});
});
用户名
:
在Ajax请求中使用数据:“id=”+username
,因为这是您在PHP中检查的POST变量
另请注意:
确保处理未设置$\u POST['username']的情况
<?php
mysql_connect("localhost","healhmate","healthmate");
mysql_select_db("hmdb");
if(isset($_POST['username'])) {// because in ajax you send username not id
$username=$_POST['username'];
$query=mysql_query("SELECT * from user where id='$username' ");
$find=mysql_num_rows($query);
echo $find;
} else {
echo "-1";
}
?>
在Ajax请求中使用数据:“id=“+username
,因为这是您在PHP中检查的POST变量
另请注意:
确保处理未设置$\u POST['username']的情况
<?php
mysql_connect("localhost","healhmate","healthmate");
mysql_select_db("hmdb");
if(isset($_POST['username'])) {// because in ajax you send username not id
$username=$_POST['username'];
$query=mysql_query("SELECT * from user where id='$username' ");
$find=mysql_num_rows($query);
echo $find;
} else {
echo "-1";
}
?>
是否也要验证密码?你的代码只检查用户名。当心Bobby Tables:你不想验证密码吗?你的代码只进行用户名检查。小心Bobby Tables:这仍然存在sql注入漏洞。这仍然存在sql注入漏洞。
<?php
mysql_connect("localhost","healhmate","healthmate");
mysql_select_db("hmdb");
if(isset($_POST['username'])) {// because in ajax you send username not id
$username=$_POST['username'];
$query=mysql_query("SELECT * from user where id='$username' ");
$find=mysql_num_rows($query);
echo $find;
} else {
echo "-1";
}
?>