即使用户名无效,登录仍会继续


The login still continues even when the username is not valid

我有以下代码,它将在授予用户访问站点的权限之前检查用户在数据库中是否可用,但是当用户输入任何值时,即使他没有在数据库中注册,它也会登录他。在此代码之后,还有 JQuery 代码,如果字段为空并且不是默认文本字段值,我还使用它来防止将任何值输入该数据库。但是,每当我登录时,我仍然开始提示输入有效文本,并继续登录过程,而不是在填写必填字段之前阻止我做任何事情。

<?php 
    if(isset($_SESSION["owner"])){
        header("Location:index.php");
        exit();
    }
?>
<?php
    require_once("includes/connection.php");
    if(isset($_POST["username"]) && isset($_POST["password"])){
        $owner = $_POST["username"];
        $password = $_POST["password"];
        $query = "SELECT id FROM users WHERE username = '$owner' AND password ='$password' LIMIT 1";
        $sql = mysql_query($query,$connection);
        $existCount = mysql_num_rows($sql);
        if($existCount == 1){   
            while($row = mysql_fetch_array($sql)){
                $id = $row["id"];
            }
            $_SESSION["id"] =$id;
            $_SESSION["owner"] = $owner; 
            $_SESSION["password"] =$password;
            echo"welcome back" .$owner."<a href='"index.php'"> please continue</a>";
            exit();
        }
        else{
            header("Location:login.php");
            exit();
        }
    }
    require_once("includes/header.php");
?>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js?ver=3.2.1'></script>
<script type='text/javascript' src='/bobs/admin/javascript/login.js'></script>
<div class="cBoth"></div>
<div id="sep"></div>
<div class="Calign">
<div id="formcontent">
<div class="flotr">
 <h2>Book faster every time</h2> 
 <br/>
 <p class="widthis">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.</p>
</div><!-- end of flotr div text --->
<div class="flotr">
<div id="errordisplay"></div>
   <fieldset class="spacing">
   <form method="post" action="admin/formProc/formproc.php" id="frmnewuser">
     <h2>New to Travelnstay ?</h2>
     <p class="widthis">A Travelnstay account is required to continue.</p> 
     <p class="formsp">Username</p>
     <p><label><input type="text" name="username" height="15px"  value="Username "id="username"/><label></p>
     <p class="formsp">Email</p>
     <p><label><input type="text" name="email" value="Your Email" id="email"/><label></p>
     <p class="formsp">Re-email</p>
     <p><label><input type="text" name="reemail" value="Retype your Email"  id="reemail"/><label></p>
     <p class="formsp">Password</p> 
     <p><label><input type="password" name="password" id="password"/><label></p>
     <p class="formsp">Re-password</p>
     <p><label><input type="password" name="repassword" id="repassword" /><label></p>
     <p class="formsp"><label><input type="submit" name="newuser" value="SING ME UP"/><label></p>
   </form>
  </fieldset>
</div><!-- end of flotr div sign up--->


<div class="flotl">
   <fieldset class="spacing">
   <form method="post" action="login.php">
   <h2>Sign in</h2>
   <p class="widthis">You need to log in to access your account.</p> 
   <p class="formsp">Username</p>
   <p><input type="text" name="username"  value="username" id="usernamelog"/></p>
   <p class="formsp">Password</p>
   <p><input type="password" name="password"  /></p>
   <p></p>
   <p class="formsp"><label><input type="submit" name="login" value="Login"/><label></p>   
   </form>
   </fieldset>
</div><!-- end of flotl div login--->

 </div>
</div>

<div class="cBoth"><!-- clear Both--></div> 
<!--<script src="/bobs/admin/javascript/formscript.js"></script>-->
<?php require_once("includes/footer.php"); ?>

下一行是 JQuery 代码。

$(document).ready(function(){
    //this will waip out anyhting in that text field.    
    $("#usernamelog").click(function(){
        $(this).val("");
        $(this).css("color","#741863");
    });          
    $("input:submit").click(function(){
        var username = $("#usernamelog").val();            
        if((username == "")||(username == "username")){
           alert("Please enter a valid username");
        }          
    });
});

您必须停止提交过程,从单击处理程序返回 false 应该可以完成此操作

  $("input:submit").click(function(){
    var username = $("#usernamelog").val();
    if((username == "")||(username == "username")){
       alert("Please enter a valid username");
       return false;
    }
  });

您需要使用 return false 来防止默认行为:

if(username == "" || username == "username") {
  alert("Please enter a valid username");
  return false;
}

你也可以使用 preventDefault() ,也可以看看 @event.preventDefault() vs. return false