确保登录php脚本

时间:2019-03-09 08:17:59

标签: php mysql

我已经建立了一个登录系统,其中insert_id和insert_password通过login.inc.php发送到XMLHttpRequest。我不确定我的php脚本是否安全。我需要一些有关脚本的安全建议。

............................................... ...............................

login.inc.php:

<?php
    session_start();
    $conn = mysqli_connect("localhost", "root", "", "users");
    $params = json_decode(file_get_contents('php://input'), true);
    $inserted_id = $params['inserted_id'];
    $inserted_password = $params['inserted_password'];

    $stmt = mysqli_stmt_init($conn);
    if (mysqli_stmt_prepare($stmt, "SELECT * FROM user WHERE account_name=? OR email=?;")) {
        mysqli_stmt_bind_param($stmt, "ss", $inserted_id, $inserted_id);
        mysqli_stmt_execute($stmt);
        $row = mysqli_fetch_assoc(mysqli_stmt_get_result($stmt));
        if ($row == null) {
            echo ("DOESNT EXISTS");
        } else {
            if (password_verify($inserted_password, $row['password'])) {
                $_SESSION['user_id'] = $row['id'];
                echo("SUCCESS");
            } else {
                echo("PASSWORD_FAIL");
            }
        }
    }
?>

signup.inc.php:

<?php
    $conn = mysqli_connect("localhost", "root", "", "users");
    $params = json_decode(file_get_contents('php://input'), true);
    $inserted_first_name = $params['first_name'];
    $inserted_last_name = $params['last_name'];
    $inserted_dob = $params['dob'];
    $inserted_email = $params['email'];
    $inserted_account_name = $params['account_name'];
    $inserted_password = $params['password'];

    $stmt = mysqli_stmt_init($conn);
    if (mysqli_stmt_prepare($stmt, "SELECT * FROM user WHERE email=?;")) {
        mysqli_stmt_bind_param($stmt, "s", $inserted_email);
        mysqli_stmt_execute($stmt);
        if (mysqli_num_rows(mysqli_stmt_get_result($stmt)) > 0) {
            echo("EMAIL_TAKEN");
        } else {
            $hashed_password = password_hash($inserted_password, PASSWORD_DEFAULT);
            $created_id = rand(111111111, 999999999);
            $stmt = mysqli_stmt_init($conn);
            if (mysqli_stmt_prepare($stmt, "INSERT INTO user(id, first_name, last_name, dob, email, account_name, password) VALUES (?, ?, ?, ?, ?, ?, ?);")) {
                mysqli_stmt_bind_param($stmt, "issssss", $created_id, $inserted_first_name, $inserted_last_name, $inserted_dob, $inserted_email, $inserted_account_name, $hashed_password);
                $result = mysqli_stmt_execute($stmt);
                echo ($result ? "SUCCESS" : "FAIL");
            }
        }
        mysqli_stmt_close($stmt);
    }
?>

1 个答案:

答案 0 :(得分:0)

安全性是整个研究领域,没有简单的方法可以衡量某些事物是否“安全”或不安全。安全通常不仅涉及代码,还涉及软件发行,组织变更等。

这是我在您发布的代码中立即注意到的第一件事:

  • 使用$_GET输入密码意味着密码未发布,因此可能存在于日志中(客户端,服务器甚至某些ISP)
  • 您的mysqli_connect呼叫使用root作为用户名。不要在生产代码中使用root。成为其他用户。
  • 您的root mysql用户.... 没有密码?!?!?!
  • 使用SELECT * FROM可能会返回比预期更多的行,尤其是因为您没有LIMIT参数。根据数据库的大小以及事物输入数据库的方式,这可能会被滥用。
  • WHERE account_name=? OR email=?-如果我使用别人的电子邮件作为帐户名怎么办?还是在“电子邮件”字段中输入另一个用户的帐户名?您的代码可能会让我访问他们的个人资料(或将我拒之门外)
  • 代码未包装在函数中,这意味着可以编辑其他PHP文件(例如扩展名)的人-可能会包含在此php文件之后-可能会看到$inserted_password变量,其中会显示密码!
相关问题