php - 制作第一个登录系统。选择 SQL 列不起作用?

标签 php mysql

我正在尝试制作我的第一个登录系统。出于某种原因,当我尝试从我的数据库中获取密码时,它没有给出值?我不确定我做错了什么。错误介于 $sql 和 $db_password 之间。

@LFlare 我不确定 DESC 用户是什么东西。这是一张 table 的照片,我不确定你想要它的样子。 http://i.imgur.com/WkZV7IZ.png

谢谢!

    <?php
    session_start();

    if (isset($_POST['login'])) {
        include_once("db.php");
        $username = strip_tags($_POST['username']);
        $password = strip_tags($_POST['password']);

        $username = stripslashes($username);
        $password = stripslashes($password);

        $username = mysqli_real_escape_string($db, $username);
        $password = mysqli_real_escape_string($db, $password);

        $password = md5($password);

        $sql = "SELECT * FROM users WHERE username = '$username' LIMIT 1";
        $query = mysqli_query($db, $sql);
        $row = mysqli_fetch_array($query);
        $id = $row['id'];
        $db_password = $row['password'];

        //echo "Password: $password";
        //echo "DB Password: $db_password";

        if ($password == $db_password) {
            $_SESSION['username'] = $username;
            $_SESSION['id'] = $id;
            header("Location: index.php");
        } else {
            echo "You didn't enter the correct details!";
        }
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        <h1>Login</h1>
        <form action="login.php" method="post" enctype="multipart/form-data">
            <input placeholder="Username" name="username" type="text" autofocus>
            <input placeholder="Password" name="password" type="password">
            <input name="login" type="submit" value="Login">
        </form>
    </body>
</html>

最佳答案

你的 PHP

<?php
    session_start();

    if (isset($_POST['username']) && isset($_POST['password'])) {
        include_once("db.php");
        $username = mysqli_real_escape_string($sqlcon, $_POST['username']);
        $password = mysqli_real_escape_string($sqlcon, $_POST['password']);
        // If you want to make sure username is alphanumeric, you can do
        // $username = preg_replace('/[^a-zA-Z0-9]/', '', mysqli_real_escape_string($sqlcon, $_POST['username']));

        // Do not use these, mysqli_real_escape_string is enough to prevent injection attacks. Furthermore, you may be compromising user security by remove special characters in passwords.
        // $username = strip_tags($_POST['username']);
        // $password = strip_tags($_POST['password']);
        // $username = stripslashes($username);
        // $password = stripslashes($password);

        // $password = md5($password); This is very susceptibile to rainbow table attacks, do something like a loop
        for ($i = 0; $i < 1000; $i ++) {
            $password = md5($password . $username); // Looping the md5 a thousand times and salting it with the username is good practice too.U
        }

        $userQuery = "SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1";
        $user = mysqli_query($sqlcon, $userQuery);

        if (mysqli_num_rows($user) > 0) { // If user exists,
            $user = mysqli_fetch_assoc($user); // mysqli_fetch_arrays put values into $user[0], $user[1], etc.
            $id = $user['id'];
            $databasepass = $user['password'];

            if ($password === $databasepass) {
                $_SESSION['username'] = $username;
                $_SESSION['id'] = $id;
                header("Location: index.php");
            } else {
                echo "Password is incorrect";
            }
        } else {
            echo "Username does not exist";
        }
    } else {
        echo "Username or Password not filled in";
    }
    echo $password;
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        <h1>Login</h1>
        <form action="login.php" method="post" enctype="multipart/form-data">
            <input placeholder="Username" name="username" type="text" autofocus>
            <input placeholder="Password" name="password" type="password">
            <input name="login" type="submit" value="Login">
        </form>
    </body>
</html>

你的 db.php

<?php
$host = "localhost";
$user = "root";
$pass = "";
$database = "users";

$sqlcon = mysqli_connect($host, $user, $pass, $database);

if (mysqli_connect_errno()) {
    die ("MySQL Database Connection Error");
}
?>

关于php - 制作第一个登录系统。选择 SQL 列不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36905291/

相关文章:

mysql - 为什么 mysql 事件计划在数据库 localhost 中不起作用?

mysql .net - 如何使用 .net 连接器更改 mysql 中的 SQL 模式

php - MySQL用逗号分隔值连接两个表

php - 如何在下拉列表中显示 sql 表(php、html)

php - 有没有办法知道哪个 PHP 文件在 Wordpress 中生成了一个页面?

php - 将数据从android手机插入mysql数据库

php - 用户在 php 中的动态表单?

mysql - 将长文本描述存储在数据库或文件系统中更好吗?

php - 使用jquery防止php文本框中的字符

javascript - jquery onclick仅用一个输入从php文件获取数据