Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用户使用php和mysql数据库登录_Php_Mysql_Login - Fatal编程技术网

用户使用php和mysql数据库登录

用户使用php和mysql数据库登录,php,mysql,login,Php,Mysql,Login,我是mysql和php新手 正在为用户创建一个带有表的数据库 我已经成功地将用户添加到数据库中,并且他们的密码使用md5(是的,我知道它不安全),它不会在线启动 我的问题是,如何根据用户正确的用户名和密码登录 这是我的密码 我的逻辑是taht,在查询运行后,它将返回true或false 如果为true,则显示登录成功,否则显示登录失败 然而,即使我输入了正确的用户名和密码,我仍然会收到一条不成功的登录消息 我检查了mysql数据库,并且uesrname正确地在那里 想法 if(!empty($_

我是mysql和php新手

正在为用户创建一个带有表的数据库

我已经成功地将用户添加到数据库中,并且他们的密码使用md5(是的,我知道它不安全),它不会在线启动

我的问题是,如何根据用户正确的用户名和密码登录

这是我的密码

我的逻辑是taht,在查询运行后,它将返回true或false

如果为true,则显示登录成功,否则显示登录失败

然而,即使我输入了正确的用户名和密码,我仍然会收到一条不成功的登录消息

我检查了mysql数据库,并且uesrname正确地在那里

想法

if(!empty($_POST['userLog']) && !empty($_POST['passLog']))
{
    //set the username and password variables from the form
    $username = $_POST['userLog'];
    $password = $_POST['passLog'];

    //create sql string to retrieve the string from the database table "users"
    $sql = "SELECT * FROM `users` WHERE userName = '$username' AND password = md5('$password')";
    $result = mysql_query($sql);
        if ($result == true) {
            $return = "<font color=#008000><Center><b>**Successful Login**</b></Center></font>";
        } else {
            $return = "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>";
        }
        print($return);
}
if(!empty($\u POST['userLog'])和&!empty($\u POST['passLog']))
{
//从表单中设置用户名和密码变量
$username=$_POST['userLog'];
$password=$\u POST['passLog'];
//创建sql字符串以从数据库表“users”检索字符串
$sql=“从`users`中选择*,其中userName='$userName'和password=md5('$password')”;
$result=mysql\u查询($sql);
如果($result==true){
$return=“**成功登录**”;
}否则{
$return=“**登录失败**”;
}
打印(返回);
}

我不完全确定您的SQL是否会运行,但为了安全起见

把它改成

$password_hash = md5($password);

$sql = "SELECT * FROM `users` WHERE userName = '$username' AND password = '$password_hash'";
对于你最初的问题

if(mysql_num_rows($result) == 1) { //If the SQL returns one row, that means that a user was found with `userName = $username` and `password = md5($password)`
    // Login
} else {
    // Authentication Failed
}

也考虑使用MySQL代替MySQL,因为它已经贬值了。

< P>首先,保护您的代码不受.< /P> 然后,确保数据库中的密码确实是使用md5()函数哈希的。 确保表单使用POST方法将数据传递给脚本

请尝试以下代码:

if(!empty($_POST['userLog']) && !empty($_POST['passLog']))
{
    //set the username and password variables from the form
    $username = $_POST['userLog'];
    $password = $_POST['passLog'];

    //create sql string to retrieve the string from the database table "users"
    $sql = "SELECT * FROM `users` WHERE userName = '". addslashes($username) ."' AND password = '". md5('$password')."'";
    $result = mysql_query($sql);
        if (mysql_num_rows($result)>0) {
            $return = "<font color=#008000><Center><b>**Successful Login**</b></Center></font>";
        } else {
            $return = "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>";
        }
        print($return);
}
if(!empty($\u POST['userLog'])和&!empty($\u POST['passLog']))
{
//从表单中设置用户名和密码变量
$username=$_POST['userLog'];
$password=$\u POST['passLog'];
//创建sql字符串以从数据库表“users”检索字符串
$sql=“从`users`中选择*,其中userName=”。addslashes($userName)。“'和password=”。md5('$password')。”;
$result=mysql\u查询($sql);
如果(mysql_num_rows($result)>0){
$return=“**成功登录**”;
}否则{
$return=“**登录失败**”;
}
打印(返回);
}

mysql\u查询
不返回TRUE或FALSE。根据docs(),如果成功,则返回一个资源;如果出现错误,则返回FALSE。您需要评估资源以查看它是否有效

if(!empty($_POST['userLog']) && !empty($_POST['passLog']))
{
    //set the username and password variables from the form
    $username = $_POST['userLog'];
    $password = $_POST['passLog'];

    //create sql string to retrieve the string from the database table "users"
    $sql = "SELECT * FROM `users` WHERE userName = '$username' AND password = md5('$password')";
    $result = mysql_query($sql);
    if ($result && $row = mysql_fetch_assoc($result)) {
        $return = "<font color=#008000><Center><b>**Successful Login**</b></Center></font>";
    } else {
        $return = "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>";
    }
    print($return);
}
if(!empty($\u POST['userLog'])和&!empty($\u POST['passLog']))
{
//从表单中设置用户名和密码变量
$username=$_POST['userLog'];
$password=$\u POST['passLog'];
//创建sql字符串以从数据库表“users”检索字符串
$sql=“从`users`中选择*,其中userName='$userName'和password=md5('$password')”;
$result=mysql\u查询($sql);
如果($result&&$row=mysql\u fetch\u assoc($result)){
$return=“**成功登录**”;
}否则{
$return=“**登录失败**”;
}
打印(返回);
}

正如我在评论中提到的,问题似乎是您的sql字符串。不是散列,而是将方法放入字符串中。所以改变

$sql = "SELECT * FROM `users` WHERE userName = '$username' AND password = md5('$password')";

您的结果不会为真或假,但由于php将任何非0的值视为真,因此它将按原样工作。
此外,强烈建议对进入sql字符串的所有数据进行转义,以防止sql注入。另一个注意事项:mysql正在被弃用,所以现在是转向mysqli之类的东西的好时机

看起来md5(密码)将成为qry字符串的一部分。尝试将其更改为
$sql=“从`users`中选择*,其中userName='$userName'和password='”。md5('$password')。“”一个快速的侧边提示是使用phpass->我尝试了这个解决方案,我输出了进入sql查询的值,它们是正确的。但是我仍然得到了一个失败的登录,并且得到了一个错误mysql_num_rows()期望参数1是resource,booleanYes我这样做了,我打印了新的passwordHash值,它匹配mysql表中的内容。这是我的错误。我忘了在
$password\u hash
上加引号。如果这仍然不起作用,您可以发布
echo$sql的值吗。哦,我没有注意到引号丢失,我已经正确输入了。但是我仍然得到了失败的登录和警告:mysql_num_rows()期望参数1是resource,boolean give这是echo$sql给我的SELECT*FROM
users
其中userName='bogus'和password='90affbd9a1954ec9ff029b7ad7183a16'
$sql = "SELECT * FROM `users` WHERE userName ='$username' AND password = '".md5('$password')."'";