如何在mysqli结果中使用html标记使用Prepared语句?

时间:2019-07-05 07:08:53

标签: php html mysqli

我最近注意到一个被称为(SQL Injection)的黑客。我使用Prepared Statements来避免这些攻击。那就是我正在使用的代码:

<?php
    session_start();
    if($_SERVER["REQUEST_METHOD"] == "POST"){
    include "inc/config.php";
    $stmt = $link->prepare("UPDATE crany_cranies SET crany_bio=?  WHERE crany_id=? ") ;
    $stmt->bind_param("si", $bio,  $id);
    
    $id= test_input($_REQUEST['id']) ;
    $bio= test_input($_POST["bio"]);
    $stmt->execute();
    
    $stmt->close();
    $link->close();
    }
    
    ?>

  <form action=editbio.php?id=<?php echo $_REQUEST[ 'id']; ?> method="post">
    <?php
    include "inc/config.php";
    $id= test_input($_REQUEST['id']);
    $sql = "SELECT crany_bio, crany_keeper FROM crany_cranies WHERE crany_id=$id";
    $result = $link->query($sql);
    
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
    //first
    if($row["crany_keeper"] != $_SESSION["id"]){
    header("location: index.php");
    }
    else{
    echo "you can use HTML tags only<br>";
    echo "<textarea name='bio' style='width:530px; font-size:11px; resize:none' cols='50' rows='15'>";
    echo $row["crany_bio"];
    echo "</textarea>";
    echo "<br><input type='submit'>";
    }
      }
    } else {
        echo "Invalid id";
    }
    $link->close();
    
    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }
    
    ?>

  </form>

那是用于编辑传记的,而我已经将“准备语句”用于第一个SQL代码。并且工作正常。但是我遇到了一个新问题,它不格式化HTML标签。并输出我们在textarea上写的任何内容

我使用了这些HTML标记,并且在使用Prepared Statements之前运行良好

<h2 style="color: white">xX Ammo Xx</h2>
<img src="http://68.media.tumblr.com/262aa3340cb98888f837e3fd82a214bd/tumblr_inline_nwr25xwGEf1sgemal_250.png">
<br>
<img src="flag/electric.png">
<br>
<p onclick="hideit()" id="click">click here!</p>
<img src="https://i.imgur.com/BSlHmyz.png">
<br> By <a href="http://flightrising.com/main.php?p=lair&tab=userpage&id=361930">Beppers!</a>
<script>
  function hideit() {
    document.getElementById("click").style.display = "none";
  }
</script>

是否有任何用于格式化标签的代码?谢谢

1 个答案:

答案 0 :(得分:0)

要允许用户输入HTML,最佳做法是按原样存储他们输入到数据库中的数据,并使用 htmlspecialchars 对数据进行编码。文本区域。当您希望将其显示为HTML时,可以按原样显示它,而不使用 htmlspecialchars 编码。您正在创建一个称为 XSS注入的新安全漏洞,尽管您的代码中没有任何内容会阻止他们输入javascript。

<?php
    session_start();
    if($_SERVER["REQUEST_METHOD"] == "POST"){
    include "inc/config.php";
    $stmt = $link->prepare("UPDATE crany_cranies SET crany_bio=?  WHERE crany_id=? ") ;
    $stmt->bind_param("si", $bio,  $id);
    
    $id= test_input($_REQUEST['id']) ;
    $bio= test_input($_POST["bio"]);
    $stmt->execute();
    
    $stmt->close();
    $link->close();
    }
    
    ?>

  <form action=editbio.php?id=<?php echo $_REQUEST[ 'id']; ?> method="post">
    <?php
    include "inc/config.php";
    $id= test_input($_REQUEST['id']);
    $sql = "SELECT crany_bio, crany_keeper FROM crany_cranies WHERE crany_id=$id";
    $result = $link->query($sql);
    
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
    //first
    if($row["crany_keeper"] != $_SESSION["id"]){
    header("location: index.php");
    }
    else{
    echo "you can use HTML tags only<br>";
    echo "<textarea name='bio' style='width:530px; font-size:11px; resize:none' cols='50' rows='15'>";
    echo htmlspecialchars($row["crany_bio"]);
    //     ^^^   HTML Tags will break the textarea if not encoded
    echo "</textarea>";
    echo "<br><input type='submit'>";
    }
      }
    } else {
        echo "Invalid id";
    }
    $link->close();
    
    function test_input($data) {
      // $data = trim($data);
      // $data = stripslashes($data);
      // $data = htmlspecialchars($data);
      //  ^^^ Don't encode data before it goes into the database
      return $data;
    }
    
    ?>

  </form>

没有看到您以前的代码,我无法想象它是如何工作的。