php登录和数据库请求

php登录和数据库请求,php,mysql,login,Php,Mysql,Login,我们有一个带有php post的表单,将通信重定向到数据库,我们想知道是否有一个简单的方法来检测失败的登录是否怀疑用户名不存在或密码不匹配。将每个错误作为单独的选项 <form id="form1" action="doLogin.php" method="post"> <div id="border2"> <table class="table_text" cellpadding="2" cellspacing="0" border="0">

我们有一个带有php post的表单,将通信重定向到数据库,我们想知道是否有一个简单的方法来检测失败的登录是否怀疑用户名不存在或密码不匹配。将每个错误作为单独的选项

<form id="form1" action="doLogin.php" method="post">
    <div id="border2">
    <table class="table_text" cellpadding="2" cellspacing="0" border="0">
        <tr>
            <td>Username:</td>
            <td><input type="text" name="username" class="input" /></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type="password" name="password" class="input" /></td>
        </tr>
        <tr>
            <td colspan="2" align="right"><a target="_new" href="register.php">Register</a><input id="login_button" type="submit" name="submit" value="Login" class="button" /></td>
        </tr>
    </table>
        </div>
</form>

用户名:
密码:
试试这个

function protect($string){
    $string = trim(strip_tags(addslashes($string)));
    return $string;
}

if($_POST['submit']){
    $username = protect($_POST['username']);
    $password = protect($_POST['password']);
    if(!$username || !$password){
        //if not display an error message
        echo "<script> alert('No data entered') </script>";
        echo "<script type='text/javascript'>document.location.href='index.php';</script>";

    }else{
        //if the were continue checking
        //select all rows from the table where the username matches the one entered by the user
        $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'");
        $num = mysql_num_rows($res);
        //check if there was not a match
        if($num == 0){
            //if not display an error message
            echo "<script> alert('User does not exist') </script>";
            echo "<script type='text/javascript'>document.location.href='index_spa.php';</script>";

        }else{
            //if there was a match continue checking
            //select all rows where the username and password match the ones submitted by the user
            $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
            $num = mysql_num_rows($res);
            //check if there was not a match
            if($num == 0){
                //if not display error message
                echo "<script> alert('Invalid Password') </script>";
                echo "<script type='text/javascript'>document.location.href='index.php';</script>";
            }else{
                //if there was continue checking
                //split all fields fom the correct row into an associative array
                $row = mysql_fetch_assoc($res);
                //check to see if the user has not activated their account yet
                if($row['active'] != 1){
                    //if not display error message
                    echo "<script> alert('Your account " . $username . " is not activated yet.') </script>";
                    echo "<script type='text/javascript'>document.location.href='index_spa.php';</script>";

                }else{
                    //if they have log them in
                    //set the login session storing there id - we use this to see if they are logged in or not
                    $_SESSION['uid'] = $row['id'];
                    //show message
                    echo "<script type='text/javascript'>document.location.href='index.php?u=" . $username . "';</script>";
                }
            }
        }
    }
}
函数保护($string){
$string=trim(strip_标记(addslashes($string));
返回$string;
}
如果($_POST['submit'])){
$username=protect($_POST['username']);
$password=protect($_POST['password']);
如果(!$username | |!$password){
//如果没有,则显示错误消息
回显“警报('未输入数据')”;
echo“document.location.href='index.php';”;
}否则{
//如果要继续检查
//从表中选择用户名与用户输入的用户名匹配的所有行
$res=mysql_查询(“从`users`中选择*,其中`username`='.$username.”);
$num=mysql\u num\u行($res);
//检查是否有匹配项
如果($num==0){
//如果没有,则显示错误消息
回显“警报('用户不存在')”;
echo“document.location.href='index_spa.php';”;
}否则{
//如果有匹配项,请继续检查
//选择用户名和密码与用户提交的匹配的所有行
$res=mysql\u查询(“从'users'中选择*,其中'username`=''.$username.''和'password`='.$password.''”;
$num=mysql\u num\u行($res);
//检查是否有匹配项
如果($num==0){
//如果没有,则显示错误消息
回显“警报(‘无效密码’)”;
echo“document.location.href='index.php';”;
}否则{
//如果有,请继续检查
//将正确行的所有字段拆分为关联数组
$row=mysql\u fetch\u assoc($res);
//检查用户是否尚未激活其帐户
如果($row['active']!=1){
//如果没有,则显示错误消息
echo“警报('您的帐户“$username.”尚未激活。”);
echo“document.location.href='index_spa.php';”;
}否则{
//如果他们已经登录了
//设置登录会话并存储id-我们使用它来查看他们是否登录
$\会话['uid']=$row['id'];
//显示消息
echo“document.location.href='index.php?u=“.$username.”;”;
}
}
}
}
}

Parse error:在“…如果登录失败,则用户名不存在…”附近出现语法错误。我从登录脚本返回一个JSON对象,详细说明所有错误代码和消息。然后可以将其与AJAX一起使用。或者,您可以简单地重定向到与此表单相同的页面,其中e=1(其中1是错误代码)。出于安全原因,您可能不希望显示如此详细的错误消息。简单地告诉他们登录失败,因为用户名/密码错误应该有足够的信息供用户确定出错的原因。-1,您的代码演示了教科书和密码。请更正您的代码以使用正确的数据库参数转义或参数化查询,并确保在将用户输入返回给用户时对其进行安全编码。您能告诉我如何操作吗?我使用php函数来保护输入字段不被注入。但在这个答案中,我一点也不写。但是-1这个???我已经编辑过了。好吗?哎呀!不,你的更新实际上让情况变得更糟。你
addslashes
从来都不是合适的工具。缓解SQL注入的一个很好的工具是参数化查询或查询。在PHP中使用MySQL时,or是一个很好的选择,因为它们提供了带有参数化查询的准备语句。如果您别无选择,只能使用令人毛骨悚然、破旧不堪的MySQL函数系列,那么至少要确保使用after。