“处理请求”,处理基本的参数化查询

时间:2019-02-18 21:05:13

标签: php sql-injection parameterized-query

我正在尝试在网上找到的东西(这是非常新的东西),但均无作用。这是我决定学习更多有关随机科学的项目,但是我仍然停留在“过程”的第二部分。 conversion tool

我观看了视频,但它们仅包含一个user_ID,而不包含用户名和密码。注意:只有处理login.php的代码才引起问题。

<?php
include("global.php");
include("db.php");

session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  // username and password are sent in the form 

  $username = $_POST['username'];
  $password = $_POST['password']; 

  // Check if the username and password exist in the database

  $sql = "SELECT username FROM users WHERE username = '$username' AND password = '$password'";
  $stmt = msqli_stmt_init($db);
  if (!mysqli_stmt_prepare($stmt, $sql)) {
    echo "SQL Statement Failed";
  } else {
      mysqli_stmt_bind_param($stmt, "ss", $username, $password );
  mysqli_stmt_execute($stmt);
   $result = mysqli_stmt_get_result($stmt);
   $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

   $count = mysqli_num_rows($result);}
  // If username and password matched then there is one row in the result

  if ($count != 0) {
     $_SESSION['login_user'] = strtolower($username);

     header("location: search.php");
  }
  else {
     $error = "Your Username or Password is invalid";
  }

} ?>

它本可以防止基本的“'or'='“注入攻击,但它决定不完全起作用。

1 个答案:

答案 0 :(得分:0)

如果使用查询参数(这绝对是个好主意),则必须在查询中保留占位符。使用?作为占位符。

赞:

$sql = "SELECT username FROM users WHERE username = ? AND password = ?";

您稍后将变量绑定到那些参数。您必须绑定与参数占位符数量相同数量的变量。

出现错误是因为您试图将变量绑定到没有参数占位符的查询。

相关问题