隐藏来自数据库的PHP URL参数

问题描述 投票:0回答:2

所以我是PHP的菜鸟,我正在尝试保护我使用PHP获取唯一页面的url参数,目前他们对跨站点脚本开放,并想知道如何解决这个问题?

 <?php  if ($result = $link->query("SELECT league_name, role, start_date, 
 end_date, joincode, active
            FROM leagues
            WHERE unique_id='$unique_id'", MYSQLI_USE_RESULT))


            while($row = $result->fetch_assoc()){ ?>
              <tbody>
             <tr>
              <td scope="row" data-label="League Name"><a class="action" href="leagueinfo.php?league_name=<?php echo $row['league_name']; ?>&joincode=<?php echo $row['joincode']; ?>"><?php echo $row['league_name'] ?></a></td>

             </tr>
            <?php }  $result->close(); ?>
          </tbody>
              </table>

              <?php mysqli_close($link); ?>

所以我需要找到一种方法来确保不会发生这种情况:

Script has been entered into url

php html mysqli xss url-parameters
2个回答
0
投票

您可以使用PDO,预准备语句提供了一种防止SQL注入的好方法:1。使用空值作为占位符准备查询。 2.将值绑定到占位符。 3.执行您的查询。

//PDO
$stmt = $link->prepare("SELECT league_name, role, start_date, end_date, joincode, active FROM leagues WHERE unique_id=:id");
$stmt->bindParam(':id', $id);
$stmt->execute();

0
投票

有几个不同的值需要编码:

  • 应该为MySQL转义$unique_id,或者应该参数化查询。 (见prepared statements。)
  • url里面的league_namejoincode应该是url编码的,这也恰好删除了html特殊字符。 (见rawurlencode
  • 锚文本中的league_name应该是html编码的(参见htmlspecialchars)。
© www.soinside.com 2019 - 2024. All rights reserved.