密码未被检查

时间:2014-05-06 07:30:07

标签: php jquery mysql ajax login

当我尝试登录时,只检查用户名而不是密码。无论我给它什么密码接受它。如果我在connect.php中将html回显为1它可以正常工作,除了重定向不起作用

dataconnect.php

<?PHP
    @mysql_connect("localhost","root","")
    or die("could not connect to mysql");
    @mysql_select_db("login")or die("no database");
?>

的index.php

<html> 
    <head> 
        <title>Animated Login</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <link rel="stylesheet" type="text/css" href="login.css">
        <script type="text/javascript">
            $(function() {
                $(".center").animate({
                   opacity: 100.0,
                   left: '+=800',
                   height: 'toggle'
               }, 5000, function(){
            });
            $(".sign_b_btn").live("click",function() {
                var u=$("#u").val();
                var p=$("#p").val();
                if(u==""){
                    $("#u").css("border-color","red");
                    $("#un").css("color","red");
                    $(".error").show().html("Please enter your username.");
                    $("#p").css("border-color","#606060");
                    $("#up").css("color","#333333");
                }else if(p==""){
                    $("#u").css("border-color","#606060");
                    $("#un").css("color","#606060");
                    $(".error").show().html("Please enter your password.");
                    $("#p").css("border-color","red");
                    $("#up").css("color","red");
                }else{
                    dataString = 'u=' + u + '&p=' + p;
                    $.ajax({
                        type: "POST",
                        url: "connect.php",
                        data: dataString,
                        cache: false,
                        success: function(html){
                            if(html == "" ){
                                $(".error").show().html("The email or password you entered ois incorrect.");
                                $("#p,#u").css("border-color","red");
                                $("#up,#un").css("color","red");
                            }if(html != ''){
                                var redirect_url = html;
                                $(".error").fadeOut(1000);
                                $("#u").css("border-color","#606060");
                                $("#un").css("color","#333333");
                                $("#p").css("border-color","#606060");
                                $("#up").css("color","#333333");
                                $(".center").animate({
                                   opacity: 0.25,
                                   left: '+=900',
                                   height: 'toggle'
                               }, 5000, function() {
                                   $(".done").slideDown(200).html("Welcome "+u); 
                                    setTimeout(function(){
                                        var u=$("#u").val("");
                                        var p=$("#p").val(""); 
                                        window.location=redirect_url;
                                    }, 5000);
                                });
                            }
                        }
                    });
                }
            }); 
        });
        </script>
    </head>
<body>
    <div class="main">
        <div class="done"></div>
        <div class="center">
            <div class="title">Login</div>
            <div class="error"></div>
            <div class="input">
                <div class="left" id="un">Username:</div>
                <div class="right">
                    <input type="text" class="log" id="u"></div><div class="c">
                </div>
            </div>
            <div class="input">
                <div class="left" id="up">Password:</div>
                <div class="right">
                    <input type="password" class="log" id="p">
                </div>
                <div class="c"></div>
            </div>
            <div class="sign_b_btn">
                <div class="sign_btn">Sign In</div>
            </div>
        </div>
    </div>
</body>
</html>

connect.php

<?PHP 
include('dataconnect.php');//Your connection to your database

//Get posted values from form
$u=$_POST['u'];
$p=$_POST['p'];

//Strip slashes
$u = stripslashes($u);
$p = stripslashes($p);

//Strip tags 
$u = strip_tags($u);
$p = strip_tags($p);

$p=md5($p);
$check = mysql_query("SELECT * FROM user WHERE user ='$u' 
AND pass='$p'")or die(mysql_error());
$check = mysql_num_rows($check);
if($check !== "0"){
$results = mysql_query("SELECT user, redirect FROM user WHERE user ='$u'") or die(mysql_error());
while ($row = mysql_fetch_assoc($results)) {
    $user=$row['user'];
    session_register('user'); 
    $_SESSION['user'] = $user;
    echo $row['redirect'];
} 
}
?>

4 个答案:

答案 0 :(得分:1)

我尽力了解你的工作,希望它有所帮助。

从您的查询

$results = mysql_query("SELECT user, redirect FROM user WHERE user ='$u'") or die(mysql_error());

WHERE子句中,您只是将用户名作为验证内容。

尝试添加密码。

$results = mysql_query("SELECT user, redirect FROM user WHERE user ='$u' AND password ='$p'") or die(mysql_error());

答案 1 :(得分:0)

您正在使用

if ($check !== "0") { ...
$check是整数时,

正在检查字符串。将其更改为:

if ($check != "0") { ...

将以字符串或整数形式检查$check0

或将其更改为

if ($check !== 0) { ...

将检查$check是否为整数0(这就是它)

答案 2 :(得分:0)

问题可能在于:

if($check !== "0"){

!==strict comparison operator,而$check将包含整数值,您将与字符串进行比较 {{1这不会导致相同的数据类型,因此永远不会输入"0"。要纠正它,你应该这样做:

if

表示返回的行数大于1.如果未找到与用户名密码的组合,则会产生0行。

答案 3 :(得分:0)

检查变量$ check中的返回值。如果我们有任何值,那么我们可以使用下一个代码。

if($check){

尝试!