防止注入:在函数中使用预准备语句和参数 - 语法错误

时间:2015-05-10 14:45:29

标签: php mysql sql-injection

我是PHP的新手,我正在尝试学习安全措施,例如防止注入。以下是我的代码。基本上我收到错误“你的SQL语法有错误;请查看与你的MySQL服务器版本相对应的手册,以便在第1行使用”1“附近的正确语法。数据仍然被发送到数据库但这个错误让我觉得注射预防不起作用。我没有错过任何{因为我正在使用Dreamweaver,它告诉我我是否至少。

另外,需要注意的是,数据库连接线和函数在一个单独的php文件中,而不是$ _POST代码中的东西。

$conn = mysqli_connect($host, $user, $password, $databaseName);

if ($_POST) { 
  if(isset($_POST['register'])){
      registerUser(           //I'm calling the function here
      $_POST['username'],
      $_POST['user_password'],
      $_POST['user_fname'],
      $_POST['user_lname'],
      $_POST['email'],
      $_POST['phone'],
      $_POST['user_birthdate'],
      $_POST['user_address1'],
      $_POST['user_address2'],
      $_POST['user_city'],
      $_POST['user_postcode'], 
      $conn);
      } 
}


function registerUser(
  $username,
  $user_password,
  $user_fname, 
  $user_lname, 
  $email, 
  $phone, 
  $user_birthdate, 
  $user_address1, 
  $user_address2, 
  $user_city, 
  $user_postcode, 
  $conn){
    if($stmt = $conn->prepare("INSERT INTO fcusers (
        username, 
        user_password, 
        user_fname, 
        user_lname, 
        email, 
        phone, 
        user_birthdate, 
        user_address1, 
        user_address2, 
        user_city, 
        user_postcode) 
        VALUES (?,?,?,?,?,?,?,?,?,?,?)")){
            $stmt->bind_param("sssssssssss",
            $username,
            $user_password,
            $user_fname, 
            $user_lname, 
            $email, 
            $phone,             
            $user_birthdate, 
            $user_address1, 
            $user_address2, 
            $user_city, 
            $user_postcode);

            $sql = $stmt->execute();
            $stmt->close();
        }
    $result = mysqli_query($conn,$sql) or die(mysqli_error($conn));
    if($result){
       echo "CONGRATS";
     }
  }

1 个答案:

答案 0 :(得分:2)

生成的错误来自你的mysqli_query(),而不是预备语句。准备好的声明使用public class Servidor_Borda { public static void main(String args[]) throws IOException{ int porta = 4004; try{ ServerSocket serverSocket = new ServerSocket(porta); Socket socketConexao; System.out.print("Aguardando conexões...\n"); while(true){ socketConexao = serverSocket.accept(); String ip = socketConexao.getInetAddress().getHostAddress(); listaAtivos.getInstance().addServidor(ip); TrataConexao trataC = new TrataConexao(socketConexao); new Thread(trataC).start(); } }catch(Exception e){ System.out.print("Porta em uso!"); } } } public class listaAtivos { private static listaAtivos singleton = null; private ArrayList<String> servidoresConectados; private listaAtivos(){ servidoresConectados = new ArrayList<>(); } public static listaAtivos getInstance(){ if(singleton==null){ singleton = new listaAtivos(); } return singleton; } public void addServidor(String ip){ servidoresConectados.add(ip); exibeAtivos(); } public String escolheServidor(){ int tam = servidoresConectados.size(); if(tam != 0){ for(int i=0;i<tam;i++){ String ip = servidoresConectados.get(i); return ip; } } return null; } } public class Cliente { public static void main(String args[]){ int porta = 4008; String ip = listaAtivos.getInstance().escolheServidor(); Socket s = new Socket(ip,porta); } } 执行。

使用$sql = $stmt->execute(),您正在运行一个返回值为mysqli_query($conn,$sql)的查询,在这种情况下,该查询被评估为1(布尔值为true),因此错误:语法在&#附近使用39; 1&#39;

您可以在$stmt->execute()上运行您的条件:

$stmt->execute()