使用 bcrypt mysqli 和类似安全性的 php sql 登录系统(防止 sql 注入(inject))

标签 php mysql security mysqli

我正在尝试建立一个带有登录系统的网站。 我在网上读过这些东西。但我发现很难把它们放在一起。而且我想我错过了很多重要的事情,比如安全。

  • mysqli
  • 验证电子邮件
  • 准备好的声明(我不知道它是什么><)
  • 除了在下面的代码中注入(inject)外,是否存在任何安全风险?

我是一名自学程序员(刚开始编码几个月,请 帮助><)

  • 我不知道 OOP 和 PDO,所以请使用过程格式 T.T
  • 不太了解安全
  • 这不是一个项目,我想发布它。所以一定有 安全性好。
  • 请帮忙改进一下,非常感谢!!!

我问了很多问题吗??

<?php
      if(isset($_POST['signup-name']) and isset($_POST['signup-password-1']) and isset($_POST['signup-password-2']) and isset($_POST['signup-email-1']) and isset($_POST['signup-email-2']) and isset($_POST['signup-country']) and isset( $_POST['recaptcha_challenge_field']) and isset( $_POST['recaptcha_response_field'])){
        if(!empty($_POST['signup-name']) and !empty($_POST['signup-password-1']) and !empty($_POST['signup-password-2']) and !empty($_POST['signup-email-1']) and !empty($_POST['signup-email-2']) and !empty($_POST['signup-country']) and !empty( $_POST['recaptcha_challenge_field']) and !empty( $_POST['recaptcha_response_field'])){

          //echo"ok1";

          $username = $_POST['signup-name'];
          $email1 = $_POST['signup-password-1'];
          $email2 = $_POST['signup-password-2'];
          $password1 = $_POST['signup-email-1'];
          $password2 = $_POST['signup-email-2'];
          $country = $_POST['signup-country'];
          $recaptcha_challenge_field = $_POST['recaptcha_challenge_field'];
          $recaptcha_response_field = $_POST['recaptcha_response_field'];

          if($email1==$email2 and $password1==$password2){


      include 'db_info.php';
      $connect = mysqli_connect("localhost", $db_uusseerrss, $db_ppwwdd) or die(mysql_error("Unable to select database"));

      $username = mysqli_real_escape_string($connect, $username);
      $email1 = mysqli_real_escape_string($connect, $email1);
      $password1 = mysqli_real_escape_string($connect, $password1);  
      $country = mysqli_real_escape_string($connect, $username);

      $bcrypt_option = array('cost'=>12);
      $hashed_password = password_hash($password1, PASSWORD_BCRYPT,$bcrypt_option);
      echo $hashed_password;

      $query = "INSERT INTO user_info (`username`, `email`, `password`, 'country') VALUES( ?, ?, ?, ?)";
      echo "ok3";
      if ($stmt = mysqli_prepare($connect, $query) ) {
        $stmt->bind_param("ssss", $username, $email1, $hashed_password, $country );
        $stmt->execute();
        echo "ok4";


              //redirect to main page
              headr(....)
            }
          }
        }
      }else{

      } 
    ?>

最佳答案

我认为一种简单的解决方案,也是在将用户值插入数据库时​​应该始终使用的一种解决方案,就是使用准备好的语句。准备好的语句允许您告诉数据库期望什么,并且会这样对待它,即使恶意用户试图抛出 sql 注入(inject)。基础知识是一个?每个参数,然后你必须用 s、i 或诸如此类的东西告诉它在 bind_param 中期望什么。希望这会有所帮助。

$query = "INSERT INTO users (`username`, `email`, `password`, 'country') VALUES( ?, ?, ?, ?)";
if ($stmt = mysqli_prepare($conn, $query) ) {
$stmt->bind_param("ssss", $username, $email1, $hashed_password, $country );
$stmt->execute();
} else {
    //I always put some sort of error message here
    ob_clean();
    header("Location: ".$_SERVER['HTTP_REFERER']);
    mysqli_close($conn);
    exit();
}

关于使用 bcrypt mysqli 和类似安全性的 php sql 登录系统(防止 sql 注入(inject)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313539/

相关文章:

php - 有没有办法从 php 调用移动应用程序

mysql - 我该如何缩短或更改此代码?

php - 如何将数组放入 SQL 中?

php - 循环遍历 ID 以显示其他表的输出

java - 小程序在安全阶段在 IE 中挂起

php - 多次mysql查询导致返回null

php - CakePhp:模型和表名问题

php - 我如何自定义重置密码验证消息以及表单验证?

mysql - 更改用户mysql密码以确保安全——最佳实践

php - 为防止 XSS 和 SQL 注入(inject)而限制的字符列表?